C# 无法访问modalpopup内gridview内的文本框
我在模式弹出窗口中有下面的网格视图,我使用edititemtemplate中的更新文本框,然后使用linkbutton触发回发事件,这样我就可以将该网格视图行的值保存在文本框中,但在代码bhind中,当我使用findcontrol时,我无法访问文本框。 请帮忙C# 无法访问modalpopup内gridview内的文本框,c#,asp.net,gridview,C#,Asp.net,Gridview,我在模式弹出窗口中有下面的网格视图,我使用edititemtemplate中的更新文本框,然后使用linkbutton触发回发事件,这样我就可以将该网格视图行的值保存在文本框中,但在代码bhind中,当我使用findcontrol时,我无法访问文本框。 请帮忙 <div id="userForm" class="form-horizontal" style="display:none;width:100%;border:solid 0px;" > <input ID=
<div id="userForm" class="form-horizontal" style="display:none;width:100%;border:solid 0px;" >
<input ID="btnshowRun" type="button" Value="Show Leave" class="btn btn-warning " style="float:lefts;" OnClick ="showhide();"/>
<asp:UpdatePanel ID="UpdatePanel5" runat="server" style="display:none;">
<ContentTemplate>
<asp:GridView ID="GridView2" runat="server" AllowSorting="False"
AutoGenerateColumns="False" DataKeyNames="EntryID"
EmptyDataText="There are no data records to display."
ShowFooter="False" HorizontalAlign="Center" OnRowCommand="gvl_RowCommand"
ShowHeaderWhenEmpty="True" CssClass="table table-striped table-bordered table-hover table-responsive table-condenseds " Width="100%" GridLines="None">
<Columns>
<asp:TemplateField HeaderText="Total" SortExpression="Total">
<EditItemTemplate>
<asp:TextBox ID="Tot" runat="server" Text='<%# Bind("Total") %>' class="form-control"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Total") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="CommandButton" runat="server" CommandArgument='<%# Eval("EntryID") %>' UseSubmitBehaviour=true CausesValidation="False"
CommandName="Update" OnCommand="Updates_Command" Text="Update" class="btn btn-primary btn-sm contrl" Style="margin-right: 10px;"
/>
<asp:LinkButton ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" class="btn btn-warning btn-sm contrl" Style="margin-right: 10px;"
Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="Button1" runat="server" CommandArgument='<%# Eval("EntryID") %>' class="form-control btn btn-default btn-blocks contrl btn-sms"
CommandName="Edit" OnCommand="Button1_Command" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Center" Font-Bold="True" ForeColor="Black"></HeaderStyle>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
您可以使用以下代码查找模板内的控件:
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView2.Rows[index];
TextBox tot = (TextBox)selectedRow.FindControl("Tot");
使用下一行使用
FindControl
方法从文本框中获取文本:
string tot = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Tot")).Text;
感谢Hamed,但是在我的例子中commandArgument不一定是rowindex,它实际上是数据库中的一个rowid,所以我得到“Index超出范围”,因为例如,行的commandArgument是5,但所选的rowindex是1
string tot = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Tot")).Text;