【问题标题】:how to get textbox value from a gridview control in a webpage using C#如何使用C#从网页中的gridview控件获取文本框值
【发布时间】:2016-05-23 13:23:45
【问题描述】:

我尝试使用以下代码从 gridview 中的文本框中获取值,但文本值显示空白“”。

这段代码有什么问题?? TextBox box1 = (TextBox)grdCountry.Rows[rowIndex].Cells[0].FindControl("TextBox1");

设计代码:
<asp:gridview ID="grdCountry" runat="server" ShowFooter="true" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="CountryName" HeaderText="Country" ItemStyle-Width="200px" /> <asp:TemplateField> <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

【问题讨论】:

  • 删除Cells[0] 部分,只需在行内搜索即可。但是你什么时候运行这个?
  • 试过了,但无济于事。没有得到这个“但是你什么时候运行这个?”
  • 在 ASP.NET 上下文中,“何时”几乎总是指页面生命周期。你在回发发生后运行它吗?在哪个页面事件,或者控件甚至处理程序中?
  • 我在回发后运行它。单击按钮时,我试图将文本框数据保存到数据库中。所以我在按钮单击控件上运行代码。

标签: asp.net gridview textbox


【解决方案1】:

尝试为文本框定义一个名称,在c#中使用“nameTextBox.Text();”

【讨论】:

  • 我认为除了 id 之外没有其他选项可以命名文本框
【解决方案2】:

问题已解决。上面的代码可以正常工作。我正在刷新整个页面,因此存储在文本框中的数据显示为空白数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多