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获取第一行。
答案 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)
希望这能解决您的问题..