Hello Steven,
I saw your code for the onLoad event for a dropdownlist
in a datagrid, and that was exactly what I needed for the
problem I had with a checkboxlist.
However, I have one problem that I've been trying to
figure out.
By the way your email address is invalid.
I have multilple rows in the grid. How do I pass the index
of that row to the onload event handler. Here's your code
and mine. The checkbox list have 3 checkbox with values B,
L and D (Breakfast, Lunch, Dinner).
Notice the TODO: in the row index.
My CODE
Public Sub EditServingType_Load(ByVal source As Object,
ByVal e As
EventArgs)
Dim servingType As CheckBoxList = CType(source,
CheckBoxList)
Select Case
CType(CheckDBNull(Me.RestaurantsDataset.Tables(0). Rows
(TODO: INDEX SHOULD GO HERE)("ServingTypeCode")),
String).Trim
Case "B"
servingType.Items(0).Selected = True
Case "L"
servingType.Items(1).Selected = True
Case "D"
servingType.Items(2).Selected = True
Case "BL"
servingType.Items(0).Selected = True
servingType.Items(1).Selected = True
Case "BD"
servingType.Items(0).Selected = True
servingType.Items(2).Selected = True
Case "LD"
servingType.Items(1).Selected = True
servingType.Items(2).Selected = True
Case "BLD"
servingType.Items(0).Selected = True
servingType.Items(1).Selected = True
servingType.Items(2).Selected = True
End Select
End Sub
Your CODE:
You can specify a
"OnLoad" event handler for the DropDownList, for example:
<asp:TemplateColumn> <ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"gender") %>
</ItemTemplate> <EditItemTemplate> <asp:DropDownList
ID="lstGender" OnLoad="lstGender_Load" Runat="server">
<asp:ListItem Value="male">Male</asp:ListItem>
<asp:ListItem Value="female">Female</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
Then, implement this handler function("lstGender_Load") in
the Page Class: protected void lstGender_Load(object
source, System.EventArgs e) { DropDownList lstGender =
(DropDownList)source; //get the datasource of the DataGrid
(you should change it to other
//if you don't use DataTAble as the datasource)
DataTable tb = (DataTable)gridTest.DataSource;
DataRow row = tb.Rows[gridTest.EditItemIndex];
if(row["gender"].Equals("male"))
{
lstGender.SelectedIndex = 0;
}
else
{
lstGender.SelectedIndex = 1;
}
}
Then, in the DataGrid's EditCommand event, you just need
to set its
EditItemIndex and rebind the data:
private void gridTest_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
gridTest.EditItemIndex = e.Item.ItemIndex;
gridTest.DataSource = Get_Data();//Get_Data() returns a
DataTable for
testing
gridTest.DataBind();
}