Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,545 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,856 people online right now. Registration is fast and FREE... Join Now!




Dynamically Populate DropDownList in Edit state of DataList

 
Reply to this topicStart new topic

Dynamically Populate DropDownList in Edit state of DataList

<ASP:codingforlife>
21 Aug, 2008 - 03:39 AM
Post #1

New D.I.C Head
*

Joined: 21 Aug, 2008
Posts: 1

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();
    }

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:53PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month