Q
Problem solve Get help with specific problems with your technologies, process and projects.

Datagrid issue: 'onselection' is not populating

A reader needs to show a Dropdownlist in page load and, based on a selection from Dropdownlist, to populate a Datagrid.

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

Daniel 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.

Dig Deeper on ASP.NET development best practices

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

TheServerSide.com

Close