【问题标题】:how to get value from boundfield then pass it to item template field?如何从 boundfield 获取值然后将其传递给项目模板字段?
【发布时间】:2013-08-02 03:03:28
【问题描述】:

在这里我很难从绑定字段到模板字段获取值..

            <asp:CommandField ShowSelectButton="True" />


            <asp:BoundField HeaderText="productid" DataField="productid" 
                SortExpression="productid" />
            <asp:BoundField HeaderText="categoryid" DataField="categoryid" 
                SortExpression="categoryid" />

        <asp:BoundField HeaderText="productname" DataField="productname" 
                SortExpression="productname" />
        <asp:BoundField HeaderText="unitsinstock" DataField="unitsinstock" 
                SortExpression="unitsinstock" />

        <asp:TemplateField>

        <ItemTemplate>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

        </ItemTemplate>

        </asp:TemplateField>
        </Columns>

这是我的代码:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {

    TextBox text_ref = (TextBox)GridView1.Rows[e.NewEditIndex].Cells[3].FindControl("TextBox2");


}

这里当用户点击gridview中的编辑链接时,boundfield值应该转移到项目模板内的文本框控件。

但上面的代码不起作用。如何做到这一点? 如何解决?

【问题讨论】:

    标签: gridview


    【解决方案1】:

    尝试调试您的代码并检查运行时值。您的代码看起来是正确的,但请确保单元格索引(代码中的 3)是模板控件所属的单元格:对我来说这是可疑的部分。

    【讨论】:

    • 是的,但是当我将“TextBox2”放在 Gridview 之外时,我得到了该单元格索引 3 的值。但它没有进入gridview。不知道怎么解决..?
    • 尝试在 RowEditing 事件中放置一个断点,然后检查(调试窗口)GridView1.Rows[e.NewEditIndex].Cells[3].Controls 对象:它应该返回分配给的控件集合细胞。我认为 Controls[1] 项应该是 TextBox 控件,但是在 VS 中检查它。不幸的是,我现在在一台 Linux 机器上,无法对此进行测试。
    • 调试时,我得到这样的值。 e.NewEditIndex=0 和 text_ref=Null
    • 那么我认为您的代码正在触发新的行事件!而不是编辑事件......你应该检查一下。
    • 我没有做 c# 代码就得到了输出。只是添加了 editemplate 而不是 itemtemplate 然后添加到了 edittemplate 内的标签上,绑定了 unitprice 值。现在它可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 2021-08-25
    • 1970-01-01
    • 2012-03-02
    • 2014-01-03
    相关资源
    最近更新 更多