Reputation: 537
I have two dropdownlists on my webform. dlName and dlStage. I have a gridview that needs to be based on the values selected from these two drop downs. Currently, I am not filtering the gridview and I am using the following code:
private void BindData()
{
string query = "select [AnnotationNumber],[AnnotationBy],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Cust,Name,AnnotationDate from vw_GridviewSource ";
SqlCommand cmd = new SqlCommand(query);
gvSummary.DataSource = GetData(cmd);
gvSummary.DataBind();
}
What I would like to do is have the gridview always check the drop downs and bind data based on the values in the drop down lists.
At the end of the select statement two values Name and AnnotationDate are the variables that would need to be matched. I included them in the query because I was trying to use a datakey to bind the data but that failed.
I am pretty new to this so I can use all the help possible.
Upvotes: 0
Views: 505
Reputation: 3289
this should be simple to implement. This is something ASP.NET will handle easily for you.
First, turn on AutoPostBack
on the DropDownLists. In the ASPX markup, add AutoPostBack="True"
inside each of the DDLs markup (same line where you see the ID
setting and runat="server"
).
Alternatively, if you want the user to make his/her selections and THEN update the GridView, you could add a button and handle the event. In that case, the AutoPostBack
wouldn't be necessary.
Second, you need to handle the change of the DDL selections, or the Button click.
If you went with the AutoPostBack
on the DDLs, make sure you handle each of the DDLs SelectedIndexChanged
events.
If you went with the Button route, handle the Button's Click
event.
This is where the two routes converge. You'll need to modify your BindData
method to accept two new parameters for each of your DDLs selected items like this:
private void BindData(string parameter1, string parameter2)
(you'll want to use more descriptive variable names)
Once you're inside, you will want to update your query to use parameterized queries. While I was typing this, @Karl Anderson added an answer that will be useful here. Use his example.
Finally, you need to go back to your event and pass the selected items for each of the DDLs to their SelectedIndexChanged event handlers.
If you go the DDL AutoPostBack route, this should work:
BindData(ddl1.SelectedValue, ddl2.SelectedValue);
(this would go in each of the SelectedIndexChanged
event handler methods)
If you go the button route, put the above code in the Click
event handler method.
Upvotes: 0
Reputation: 34846
Try this:
// Both drop down lists need a selected value or we don't do the query and bind
if(!String.IsNullOrEmpty(dlName.SelectedValue) &&
!String.IsNullOrEmpty(dlStage.SelectedValue)
{
string selectedName = dlName.SelectedValue;
string selectedAnnotationDate = dlStage.SelectedValue;
// Use parameterized SQL instead of in-line SQL to avoid SQL Injection risks
SqlCommand cmd = new SqlCommand();
cmd.Connection = YourConnectionObject;
cmd.CommandText = "select [AnnotationNumber],[AnnotationBy],[AnnotationType],
[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,
[sgkComments],[ActualAgencyError],Cust,Name,AnnotationDate from
vw_GridviewSource where Name = @p1 and AnnotationDate = @p2";
cmd.Parameters.Add("@p1", SqlDbType.VarChar, 100, selectedName);
cmd.Parameters.Add("@p2", SqlDbType.VarChar, 100, selectedAnnotationDate);
gvSummary.DataSource = GetData(cmd);
gvSummary.DataBind();
}
Note: Substitute the correct database types and sizes for @p1
and @p2
above.
Upvotes: 1