[英]How to display selected data from dropdownlist in grid view?
I have a dropdownlist
in which I want to show different branches of a college. 我有一个
dropdownlist
,我想在其中显示大学的不同分支。
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. 当用户从
dropdownlist
选择值之一时,则必须在网格视图中显示与该值相对应的数据。
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. 例如,当用户从下拉列表框中选择“信息技术”时,则必须在网格视图中显示与信息技术相关的教职员工列表。
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 将代码写在dropdown所选索引已更改的方法上,该方法将通过获取值将数据绑定到gridview并将dropdown属性autopostback设置为true
.aspx file .aspx文件
<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 您的C#代码
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
}
Here's how I would do it : 这是我的做法:
1) Use a global datatable variable to store the complete data. 1)使用全局数据表变量存储完整数据。 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. 2)在加载时,使用该变量填充gridview,也就是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. 3)在您的下拉列表中附加一个“ onchange”事件。 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.
在该事件的实现中,您将捕获选定的值,并可以使用它使用默认dataview的rowfilter属性来过滤您的数据表(全局)。 Once done, all you need is firing a new databind() , binding this filter data back to your gridview
完成后,您需要启动一个新的databind(),将此过滤器数据绑定回您的gridview
Hope tis helps... 希望这有助于...
Now whenever you would select a value from dropdown the gridview would be populated respectively. 现在,只要您从下拉列表中选择一个值,就会分别填充gridview。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.