Hi,
I have a Datalist which needs to have a populated dropdownlist created when the EditCommand is called. I have my dropdownlistcode in the ItemDataBound function. Is this the correct place to have it?
When I run the code and click my edit button, my dropdown contains no values and when I debug the code the line - DropDownList ddoffence = (DropDownList)e.Item.FindControl("ddoffence"); - gives a null value.
I need to populate the dropdownlist in the backend because after Ive gotten this part working I need to then manipulate sql result set to check for null values.
Any help is much appreciated.
CODE
[b]Front End:[/b]
<asp:DataList EnableViewState="true" ID="DataList1" runat="server" Width="100%" DataKeyField="ID" DataMember="DefaultView" DataSourceID="SqlDataSource2" CellPadding="0" GridLines="None" ShowFooter="False" OnEditCommand="DataList1_Edit" OnUpdateCommand="DataList1_Update" OnCancelCommand="DataList1_Cancel" OnDeleteCommand="DataList1_Delete" OnItemCommand="DataList1_ItemCommand">
<HeaderTemplate></HeaderTemplate>
<itemtemplate>
Offence</div><%# DataBinder.Eval (Container.DataItem, "Offence")%>
<asp:ImageButton Width="81" Height="18" AlternateText="Edit" ImageUrl="images/BTN-green_edit.png" ID="LinkButton3" runat="server" CommandName="Edit"></asp:ImageButton>
</itemtemplate>
<EditItemTemplate>
Offence<asp:DropDownList ID="ddoffence" runat="server" AutoPostBack="true">
</asp:DropDownList>
<asp:ImageButton Width="81" Height="18" AlternateText="Update" ImageUrl="images/BTN-green_update.png" ID="LinkButton4" runat="server" CommandName="Update"></asp:ImageButton>
</EditItemTemplate>
<FooterTemplate></FooterTemplate>
[b]Code Behind:[/b]
protected void DataList1_ItemDataBound(object sender, DataListCommandEventArgs e)
{
DataList DataList1 = (DataList)sender;
sql = "SELECT ID, description, code FROM offence";
ds = new DataSet();
da.FillDataSetFromSql(sql, ds);
DropDownList ddoffence = (DropDownList)e.Item.FindControl("ddoffence");
ddoffence.DataSource = ds.Tables[0];
ddoffence.DataTextField = "description";
ddoffence.DataValueField = "ID";
ddoffence.DataBind();
}