I need to show a Dropdownlist in page load, and based on my selection from Dropdownlist I need to populate a Datagrid , the Dropdownlist is independent of the datagrid . Is it possible to do that.
Please note that the Dropdownlist is independent and not part of the grid .
Based on the selection from the list the grid will be populated.
if(!page.Ispostback)
{
SqlConnection myConnection = new SqlConnection("server=MANSV107;database=EPPRD;uid=kpi_user;
pwd=kpi_user"); myConnection.Open(); SqlDataAdapter myCommand = new SqlDataAdapter("select
name from Plants where p_id not in(5)",myConnection); //SqlDataAdapter myCommand
= new SqlDataAdapter("select * from kpi_conscomp_v where plant like 'Manchester%'and
year='2004'", myConnection); DataSet ds =new DataSet(); myCommand.Fill(ds,"kpi");
DropDownList1.DataSource=ds;
DropDownList1.DataTextField="name";
DropDownList1.DataBind();
/********************************/
// form = (HtmlForm) this.FindControl("Form1"); // form.Controls.Add(
dropdownlist1 ); // DropDownList1.Style["POSITION"]
= "absolute"; DropDownList1.Style["TOP"]
= "120"; }
It is working fine but onselection from the List the Datagrid is not populating.
The code is private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e) { SqlConnection myConnection = new SqlConnection("server=MANSV107;database=EPPRD;uid=kpi_user; pwd=kpi_user"); myConnection.Open();
SqlDataAdapter da=new SqlDataAdapter("select * from kpi_conscomp_v where
plant like'" + DropDownList1.SelectedValue +"'",myConnection);
DataSet ds=new DataSet(); da.Fill(ds,"KPI"); MyDataGrid.DataSource=ds.Tables["KPI"].DefaultView;
MyDataGrid.DataBind();
}
Requires Free Membership to View
When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.
Hannah Smalltree, Editorial DirectorDaniel Cazzulino responds:
There are a couple things to keep in mind.
First, if you dropped the dropdownlist on the design surface, you don't need to call form.Controls.Add. If you haven't, and you're creating the control programmatically, you just need to do this: Controls.Add (it will call the Page.Controls.Add).
Secondly, DropDownList does not postback by default when a selection is made. If you want that to happen, just set the AutoPostBack property to 'true,' and the SelectedIndexChanged event will be fired right away when the user changes the selection on the client side.
This was first published in July 2005