【问题标题】:Can't reach a control from EditItemTemplate in a GridView无法从 GridView 中的 EditItemTemplate 访问控件
【发布时间】:2012-12-31 10:19:44
【问题描述】:

我在尝试更新我的 gridview 时找不到我想要的控件。控件是来自 EditItemTemplate 的文本框和下拉列表。但是,如果我尝试在 ItemTemplate 中找到一个标签,它就可以正常工作。问题似乎是它在我有机会获得控件之前“跳出”编辑模式。

我的网格视图的标记:

<asp:GridView ID="ProductGridView" runat="server" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" DataKeyNames="Id" OnRowEditing="ProductGridView_RowEditing"
    OnRowCancelingEdit="ProductGridView_RowCancelingEdit" OnRowUpdating="ProductGridView_RowUpdating"
    OnRowDeleting="ProductGridView_RowDeleting" OnRowDataBound="ProductGridView_RowDataBound">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CausesValidation="false" />
        <asp:TemplateField HeaderText="Name" SortExpression="Name">
            <EditItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
            <EditItemTemplate>
                <asp:TextBox ID="txtQuantity" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Family" SortExpression="Family.Name">
            <EditItemTemplate>
                <asp:DropDownList ID="ddlFamily" runat="server" OnInit="ddlFamily_Init">
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblFamily" runat="server" Text='<%# Eval("Family.Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这是我的 RowUpdating 方法的代码隐藏。如何从 EditItemTemplate 访问控件?我错过了一些非常简单的东西吗?

protected void ProductGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {            
        // Get the controls

        GridViewRow row = ProductGridView.Rows[e.RowIndex];
        Label lblName = (row.FindControl("lblName") as Label); // Returns this label, from the row being edited, just fine
        TextBox txtName = (row.FindControl("txtName") as TextBox); // null
        TextBox txtQuantity = (row.FindControl("txtQuantity") as TextBox); // null
        DropDownList ddlFamily = (row.FindControl("ddlFamily") as DropDownList); // null

        // More code to populate product etc.
    }

protected void ProductGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        ProductGridView.EditIndex = e.NewEditIndex;
        BindGridView(_productRepo.GetAll());
    }

【问题讨论】:

    标签: c# asp.net gridview edititemtemplate


    【解决方案1】:

    代替commandfield,尝试使用like

     <asp:TemplateField HeaderText="Action" HeaderStyle-Width="20%" ItemStyle-HorizontalAlign="Center">
                    <ItemTemplate>
                        <asp:LinkButton ID="LnkManageTitle" runat="server" Text="Manage Title" CommandName="Edit"></asp:LinkButton>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="LnkManageTitle" runat="server" Text="Save" CommandName="Update"></asp:LinkButton>
                    </EditItemTemplate>
                </asp:TemplateField>
    

    休息似乎很好。

    要更新,请在 html 页面和 CS 页面上调用 OnRowUpdating="Gridview1_RowUpdating" 声明事件。

    Protected Void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    }
    

    别忘了在LinkButton 上致电CommandName="Update"

    【讨论】:

    • 我已经尝试了这个稍作改动,即每个模板中有两个链接按钮而不是一个。在 ItemTemplate 中编辑和删除,在 EditItemTemplate 中更新和取消。当我单击编辑时,gridviewrow 进入编辑模式,但是当单击更新时,行编辑不会触发。想法?删除一行并取消编辑工作正常。
    • 对于更新:您必须调用网格视图的 RowUpdating 事件..参见上面的编辑代码。
    • 这就是我的意思。我已经按照你的意思做了,但它仍然不起作用。我的页面上有ViewStateMode="Disabled",因为我遇到了一个我无法以任何其他方式解决的错误。你觉得跟这个有关系吗?
    • 我解决了这个问题。我用上面的代码创建了一个新的网络表单,它工作正常!
    【解决方案2】:
    protected void ProductGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {            
        // Get the controls
        GridViewRow row = (GridViewRow)ProductGridView.Rows[e.RowIndex];
        TextBox tname = (TextBox)row.FindControl("txtName");
    
        // More code to populate product etc.
    }
    

    【讨论】:

    • 感谢您的快速回复。但是,我的代码中已经有了这个。将添加到我的原始帖子中。
    • tname 仍然为空。在 EditItemTemplate 中找不到任何控件。
    • 在RowEditing 中设置gridview 编辑索引。如下 ProductGridView.EditIndex = e.Row.RowIndex;
    • 我已将编辑索引设置为:ProductGridView.EditIndex = e.NewEditIndex;。您提到的代码是不可能的,因为 GridViewEditEventArgs 不包含 Row 属性。
    【解决方案3】:

    也许?

    protected void ProductGridView_RowUpdating(object sender, GridViewUpdateEventArgs e){
                Label lblName = (Label)ProductGridView.Rows[e.RowIndex].FindControl("lblName");
                TextBox txtName = (TextBox)ProductGridView.Rows[e.RowIndex].FindControl("txtName"));
                TextBox txtQuantity = (TextBox)ProductGridView.Rows[e.RowIndex].FindControl("txtQuantity"); 
                DropDownList ddlFamily = (DropDownList)ProductGridView.Rows[e.RowIndex].FindControl("ddlFamily"); 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多