Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspdotnet_website_programming thread: dropdownlist dynamically selected item


Message #1 by "Valerie Andersen (MERRYMAN)" <vande@m...> on Tue, 23 Apr 2002 13:22:49 -0700
Adding more info . . . .


I have a Datagrid in an ASPX/C#  page that allows editing rows through
the use of an edit button.  When the user clicks the edit button, the
row appears with text boxes & dropdownlist controls to allow the user to
pick items from.  The dropdownlist is populated from a dataset.  This is
working nicely, but I need to set the  selected item to the existing
value.  Can anyone share any example code on how to do this?  I have
tried looping through the dropdownlist items in the function called with
the OnLoad event but, it seems to do nothing.  Can anyone offer any
advice?

Related code listed below.

Thanks,
Valerie

						<EditItemTemplate>
=09
<asp:dropdownlist
=09
runat=3D"server"
=09
id=3D"ddl_type"
=09
DataValueField=3D'type'
=09
DataTextField=3D'type'
=09
DataSource=3D'<%# dsType %>'
OnLoad=3D'<%# ddl_TypeSelected %>'>
=09
</asp:DropDownList>
						</EditItemTemplate>


		public void ddl_TypeSelected(string sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			DropDownList ddl =3D
((DropDownList)e.Item.FindControl("ddl_type"));
			foreach (ListItem item in ddl.Items)
			{
				if (item.Value.ToString() =3D=3D
e.Item.Cells[3].ToString());
				{
					item.Selected =3D true;
					break;
				}
			}

		}


-----Original Message-----
From: Valerie Andersen (MERRYMAN) [mailto:vande@m...]
Sent: Tuesday, April 23, 2002 9:57 AM
To: Website Programming with ASP.NET
Subject: [aspdotnet_website_programming] Populate dropdown from database
in a datagrid when editing a row

Thanks - I actually got the dropdown populated successfully using a
dataset and just setting the datasource value of the dropdown to that
dataset through aspx i.e.

						<EditItemTemplate>
=09
<asp:dropdownlist
=09
runat=3D"server"
=09
id=3D"ddl_type"
=09
DataValueField=3D'type'
=09
DataTextField=3D'type'
=09
DataSource=3D'<%# dsType %>'>
=09
</asp:DropDownList>
						</EditItemTemplate>

Now my problem is setting the selected value to match the value
displayed in the cell before the user clicked the edit button - I can't
seem to get at the dropdownlist to set any of it's values, I am assuming
because it is embedded in the datagrid.  Does anyone have any examples
of doing this that have worked?

Thanks,
Valerie


-----Original Message-----
From: vop@i... [mailto:vop@i...]
Sent: Tuesday, April 23, 2002 2:29 AM
To: Website Programming with ASP.NET
Subject: [aspdotnet_website_programming] Re: Populate dropdown from
database in a datagrid when editing a row

Try this one:

Control cGameCategory =3D 
EditReview.Items[0].FindControl("GameCategory");
((DropDownList) cGameCategory).DataTextField  =3D "txt_kategoria";
((DropDownList) cGameCategory).DataValueField =3D "id_kategoria";

string Sql =3D "SELECT * FROM categories ORDER BY txt_kategoria ASC";
SqlDataAdapter Adapter =3D new SqlDataAdapter(Sql, Spojenie);
DataSet        Ds      =3D new DataSet();
Adapter.Fill(Ds, "categories");
((DropDownList) cGameCategory).DataSource =3D Ds.Tables["categories"];
((DropDownList) cGameCategory).DataBind();

EditReview -> my DataGrid
GameCategory -> my DropDownList


  Return to Index