0

I have a dropdownlist in which I want to show different branches of a college.

When a user selects one of the value from the dropdownlist then the data corresponding to that value has to be displayed in the grid view.

For example when a user selects "information technology" from drop-down list box then list of faculty members related to information technology has to be displayed in grid view.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Anurag Jain
  • 1,371
  • 4
  • 23
  • 34

3 Answers3

1

write your code on dropdown selected index changed method that will bind the data to the gridview by getting the value and set the dropdown property autopostback to true

.aspx file

<asp:DropDownList id="ddlBranch" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlBranch_onSelectIndexChanged"/>
<asp:ListItem Value="1">Finance</asp:ListItem>
<asp:ListItem Value="2">Information Technology</asp:ListItem>
</asp:DropDownList>
<br/>
<br/>

<asp:GridView id="GridView1" runat="server">

Your c# Code

protected void ddlist_onSelectIndexChanged(object sender, EventArgs e)
 {
           string selectedBranch=ddlBranch.SelectedItem.Text;

           DataSet dsBranchDetails=GetDataForBranch(selectedBranch);

            GridView1.DataSource=dsBranchDetails;
            GridView1DataBind();
 }


public DataSet GetDataForBranch(string selectedBranch)
{
 //     your code
}
Nitin Aggarwal
  • 451
  • 1
  • 7
  • 18
  • How do I handle the `//your code` part? All I am doing is running a SQL query from the text? – SearchForKnowledge Aug 08 '14 at 12:43
  • If you are getting data from SQL then return this as a DataSet, rest is the same, follow this link to know more.. http://stackoverflow.com/questions/6584817/direct-method-from-sql-command-text-to-dataset – Nitin Aggarwal Aug 08 '14 at 18:49
0

Here's how I would do it :

1) Use a global datatable variable to store the complete data. It will contain your whole database table (something like "Select * from facultymembers")

2) On load, use that variable to populate a gridview, aka your table in aspx. Use it also to build the list of items of you selectbox, by extracting distinct values of the college branches and binding it to the dropdownlist see this post for code

3) Attach an "onchange" event to your dropdownlist. In the implementation of that event, you'll catch the selected value and can use it to filter your datatable (which is global) using the rowfilter property of the default dataview. Once done, all you need is firing a new databind() , binding this filter data back to your gridview

Hope tis helps...

Community
  • 1
  • 1
Laurent S.
  • 6,816
  • 2
  • 28
  • 40
0
  1. Bind your gridview to a datasource
  2. Define a control parameter on your datasource (object datasource/SQL datasource etc.) & set the control parameter to your dropdown list
  3. Set AutoPostback property of your dropdown list to true

Now whenever you would select a value from dropdown the gridview would be populated respectively.

Zo Has
  • 12,599
  • 22
  • 87
  • 149