简体   繁体   English

如何在网格视图中显示下拉列表中的选定数据?

[英]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... 希望这有助于...

  1. Bind your gridview to a datasource 将您的gridview绑定到数据源
  2. Define a control parameter on your datasource (object datasource/SQL datasource etc.) & set the control parameter to your dropdown list 在数据源(对象数据源/ SQL数据源等)上定义控制参数,并将控制参数设置为下拉列表
  3. Set AutoPostback property of your dropdown list to true 将下拉列表的AutoPostback属性设置为true

Now whenever you would select a value from dropdown the gridview would be populated respectively. 现在,只要您从下拉列表中选择一个值,就会分别填充gridview。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于下拉列表中的选定值在文本框中显示数据 - How to display data on textbox based on selected value from dropdownlist 如何在实体框架中连接两个或多个表并在数据网格视图中显示每个表中的选定列 - How to join two or more tables in entity framework and display selected columns from each tables in data grid view 如何基于三个下拉列表的选择显示网格视图 - How to display grid view based on the selection of three dropdownlist 如果从网格视图中删除了项目,如何从下拉列表中删除 - How to delete from the dropdownlist if an item is deleted from grid view 如何从数据库中提取数据并显示在下拉列表中 - How to pull data from database and display in dropdownlist 如何在表单提交后显示从下拉列表中选择的值 - How to display selected value-from-dropdownlist-after form submit 如何将选定的行从网格视图绑定到另一个网格视图 - How to Bind Selected Rows From Grid View to Another Grid View 如何将选定下拉列表的值从视图传递到控制器 - How to pass value of selected dropdownlist from view to the controller 如何从视图中的 JQuery 下拉列表中获取所选值到 Controller - How to get selected value from JQuery Dropdownlist in View to Controller 使用手动数据在网格视图中填充下拉列表 - Filling a dropdownlist in a grid view with manual data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM