【问题标题】:Binding data to a textbox , inside a templatefield of a gridview » C# and ASP.NET将数据绑定到文本框,在 gridview 的模板字段内 » C# 和 ASP.NET
【发布时间】:2010-06-23 14:12:21
【问题描述】:

如何将数据绑定到 gridview 的模板字段中的文本框? 我想使用 ExecuteScalar ,获取一个值并将其扔到那个文本框。

【问题讨论】:

    标签: c# asp.net gridview textbox templatefield


    【解决方案1】:

    基本上,您创建一个方法来返回您的值,然后在数据绑定表达式中调用它。看看这个例子:

    在您的 aspx 页面中调用数据绑定表达式中的函数 GetValue:

    <asp:GridView ID="GridTest" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID="txtValue" Width="200px" runat="server" Text='<%#GetValue((int)Container.DataItem)%>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    那么在你后面的代码中你就有了获取值的函数:

    protected void Page_Load(object sender, EventArgs e)
    {
    
        GridTest.DataSource = new List<int>{1, 2, 3};
        GridTest.DataBind();
    
    }
    
    protected string GetValue(int ID)
    {
        return "Value from Execute Scalar " + ID;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多