按钮在gridview中更新并从隐藏文本框中获取值

时间:2017-05-15 03:59:50

标签: asp.net vb.net

gridview中的按钮无法从隐藏的文本框中获取正确的值。

以下是代码:

ASP:

<columns>
  <asp:TemplateField >
      <ItemTemplate>
 <asp:TextBox ID="txtFruit" runat="server" Visible="true" Text='<%# Bind("txtFruit")%>'  />
      </ItemTemplate>
   </asp:TemplateField>

     <asp:BoundField DataField="HiddentxtFruit" HeaderText ="Fruit" HeaderStyle-Width="0%" HtmlEncode="false" Visible="false"   />

     </asp:TemplateField>


            <asp:TemplateField HeaderText="Confirm">
 <ItemTemplate>
<asp:Button ID="btnSubmit" Text="Confirm" runat="server"  CommandName="Submit" />

 </ItemTemplate>
 </asp:TemplateField>

</columns>
代码背后的代码:

 For intX As Integer = 0 To gridview1.Rows.Count - 1
   Dim txtPLACE As TextBox = TryCast(gridview1.Rows(0).Cells(0).FindControl("txtFruit"), TextBox)
Next

 For intX As Integer = 0 To gridview1.Rows.Count - 1
   Dim txtPLACEss As TextBox = DirectCast(gridview1.Rows(intX).FindControl("txtFruit"), TextBox)
Next

这只会从gridview获取第一行。

1 个答案:

答案 0 :(得分:0)

使用下面的代码来获取行的索引:

Dim rowIndex = DirectCast(DirectCast(sender, Control).NamingContainer, GridViewRow).RowIndex

然后

 Dim txtPLACEss As TextBox = DirectCast(gridview1.Rows(rowIndex).FindControl("txtFruit"), TextBox)

这将解决您的问题

更新:

 <asp:TemplateField >
  <ItemTemplate><asp:TextBox ID="txtFruit" runat="server" Visible="true" Text='<%# Bind("txtFruit")%>'  /></ItemTemplate></asp:TemplateField>
<asp:TextBox ID="txtFruitHidden" runat="server" Text='<%# Bind("txtFruit")%>'  HeaderStyle-Width="0%" HtmlEncode="false" Visible="false" />

 </asp:TemplateField>

然后在C#

     Dim txtPLACEss As TextBox = DirectCast(gridview1.Rows(rowIndex).FindControl("txtFruitHidden"), TextBox)

希望这能解决您的问题..