问题:如何将参数传递给asp .net 4.5中gridView的SelectMethod
描述:
我正在使用 asp .net 4.5 并使用(强类型模型绑定(bind))SelectMethod 将数据绑定(bind)到 gridView。
SelecteMethod="BindGrid" BindGrid 是用户定义的函数。
但是在绑定(bind)时我想将 GridRow 作为参数传递给函数 BindGrid。
那么有什么方法可以将参数传递给 selectMethod 来进行强类型模型绑定(bind)吗?
最佳答案
有一个great example说明如何使用控件“过滤”从“SelectMethod”返回的数据。例如,如果您要更改下拉列表中所选内容的数据。您可以像这样设置标记...
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlEmployee" AutoPostBack="true" runat="server">
<asp:ListItem Text="1"></asp:ListItem>
<asp:ListItem Text="2"></asp:ListItem>
</asp:DropDownList>
</div>
<div>
<asp:GridView ID="grdEmployee" runat="server" SelectMethod="GetEmployees" ItemType="WebApplication2.Employee">
</asp:GridView>
</div>
</form>
然后该方法将类似于接收参数...
public List<Employee> GetEmployees([Control]int? ddlEmployee)
{
List<Employee> employeeList = new List<Employee>();
for (int i = 1; i <= 5; i++)
{
employeeList.Add(new Employee {
EmployeeId=i,
FirstName=string.Format("First{0}",i),
LastName=string.Format("Last{0}",i)
});
}
return employeeList.Where(e=>e.EmployeeId==ddlEmployee).ToList();
}
根据您的具体情况,此方法有多种变体。希望这有帮助!
关于parameters - 如何在asp .net 4.5中将参数传递给gridView的SelectMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15779358/