1 Replies - 11727 Views - Last Post: 10 March 2009 - 12:47 PM Rate Topic: -----

#1 <ASP:codingforlife>   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-August 08

Dynamically Populate DropDownList in Edit state of DataList

Posted 21 August 2008 - 04:39 AM

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.

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



Is This A Good Question/Topic? 0
  • +

Replies To: Dynamically Populate DropDownList in Edit state of DataList

#2 tolkarovers   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 10-March 09

Re: Dynamically Populate DropDownList in Edit state of DataList

Posted 10 March 2009 - 12:47 PM

((DropDownList)e.Item.FindControl("CategoryDropDown")).SelectedIndex = ((DropDownList)e.Item.FindControl("CategoryDropDown")).Items.IndexOf(((DropDownList)e.Item.FindControl("CategoryDropDown")).Items.FindByText(category.Rows[0]["categoryName"].ToString()));

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1