【问题标题】:How to pass parameter to SelectMethod of gridView in asp .net 4.5如何在asp .net 4.5中将参数传递给gridView的SelectMethod
【发布时间】:2013-04-03 05:14:04
【问题描述】:

问题:如何在 asp .net 4.5 中将参数传递给 gridView 的 SelectMethod

说明:

我正在使用 asp .net 4.5 并使用(强类型模型绑定)SelectMethod 将数据绑定到 gridView。

SelecteMethod="BindGrid" BindGrid 是用户定义的函数。

但是在绑定时我想将 GridRow 作为参数传递给函数 BindGrid。

那么有什么方法可以将参数传递给 selectMethod 进行强类型模型绑定?

【问题讨论】:

    标签: parameters asp.net-4.5


    【解决方案1】:

    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();
        }
    

    根据您的具体情况,此方法有多种变化。希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      传参一般有两种方式:

      • 以声明方式来自 SelectParameters 元素。
      • 通过Selecting 方法以编程方式实现。

      here

      我认为在你的情况下你必须选择Selecting 方法。

      【讨论】:

        【解决方案3】:

        MSDN > 任务 3 - 模型绑定中的值提供者 > (5):https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/hands-on-labs/whats-new-in-web-forms-in-aspnet-45#task-3---value-providers-in-model-binding

        public IQueryable<Category> GetCategories([Control("controlId")] int? minProductsCount)
        {
            var query = this.db.Categories.Include(c => c.Products);
        
            if (minProductsCount.HasValue)
            {
                query = query.Where(c => c.Products.Count >= minProductsCount);
            }
        
            return query;
        }
        

        【讨论】:

          猜你喜欢
          • 2023-01-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-05
          • 2013-03-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多