【问题标题】:How to set boundfield value to template field control?如何将boundfield值设置为模板字段控件?
【发布时间】:2013-08-02 17:26:29
【问题描述】:

我有这样的 gridview 输出。

当我点击编辑链接时,我需要将unitsinstock 值传递给文本框控件。它存在于模板字段中。

这是我的 C# 代码:

TextBox tt = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox2").ToString();

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

TextBox3.Text = text_ref.Text;

有什么问题吗?当我调试e.NewEdtIndex=0TextBox3=null。如何解决?

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您应该为此使用EditItemTemplate。例如,当您编辑行时,上面的代码会将 unitsinstock 值绑定到 unitsinstock 副本 文本框。

    同时更改 SqlDataSource:

    <UpdateParameters>
        <asp:Parameter Name="unitsinstock" Type="Int32" /> /*Put the correcly type here*/
        /*other fields*/
    </UpdateParameters>
    

    还有 GridView:

    <asp:TemplateField HeaderText="unitsinstock">
      <ItemTemplate>
    <asp:label id="labelUnitsinstock" runat="server" text='<%#Eval("unitsinstockActual")%>'>
        </asp:label>
      </ItemTemplate>
    </asp:TemplateField>
    
    <asp:TemplateField HeaderText="unitsinstock copy">
      <EditItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server"
         Text='<%# Bind("unitsinstock") %>'></asp:TextBox>
      </EditItemTemplate>
    </asp:TemplateField>
    

    在这里查看更多信息Using TemplateFields in the GridView Control

    【讨论】:

    • 您应该发布更多 c# 代码来提供帮助!但是,使用 GridView 中的 EditTemplate 执行此操作更有意义,更少硬编码!
    • 嗨,Fals,上面的代码工作正常。但是当我更新数据库中的值时,这些值没有更新。任何解决方案...
    • 其实我是用Sql Datasource来更新数据的。通过使用 SqlDatasource 我创建了更新命令。当我更新没有任何模板字段的数据时,它可以正常工作,但是当使用模板字段时它不会更新
    猜你喜欢
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多