【问题标题】:Cant access textbox inside gridview inside modalpopup无法访问modalpopup内gridview内的文本框
【发布时间】:2017-09-11 10:25:05
【问题描述】:

我在模态弹出窗口中有以下网格视图,我使用更新编辑项模板内的文本框,然后使用链接按钮触发回发事件,以便我可以将值保存在该网格视图行的文本框中,但在代码隐藏时我使用 findcontrol 时无法访问文本框。 请帮忙

<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;"
                                />
                            &nbsp;<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>

这就是在 C# 中访问控件的方式

protected void Updates_Command(object sender, CommandEventArgs e)
{
    Control p = ((Control)sender).Parent;

    int tot = int.Parse(((TextBox)p.FindControl("Tot")).Text); 
    int rowid = int.Parse(Session["RowID"].ToString());
    int suc = Database.ExecuteNonQuery("UPDATE LeaveAssignmentEntry SET Total='" + tot + "' WHERE Id = " + rowid + "");

    if (suc==1)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('Successful')", true);

    }
}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您可以使用此代码在模板中查找控件:

    int index = Convert.ToInt32(e.CommandArgument);    
    GridViewRow selectedRow = GridView2.Rows[index];
    TextBox tot = (TextBox)selectedRow.FindControl("Tot");
    

    【讨论】:

    • 谢谢哈米德,但在我的情况下,commandArgument 不一定是 rowindex,它实际上是数据库中的 rowid,所以我得到“索引超出范围”,因为例如该行的 commandargument 是 5但选定的 rowindex 是 1
    【解决方案2】:

    使用下面的行通过FindControl 方法从TextBox 中获取文本:

    string tot = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Tot")).Text;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多