无法从GridView中的EditItemTemplate获取控件

本文关键字:EditItemTemplate 获取 控件 中的 GridView | 更新日期: 2023-09-27 17:50:27

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

我的gridview的标记:

<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方法的代码。如何从edittitemtemplate访问控件?我是不是错过了一些非常简单的东西?

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());
    }

无法从GridView中的EditItemTemplate获取控件

代替命令域,尝试使用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>

休息好了。

对于更新,调用OnRowUpdating="Gridview1_RowUpdating"在html页面和CS页面声明一个事件。

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

不要忘记在LinkButton上调用CommandName="Update"

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.
}

可能?

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"); 
}
相关文章:
  • 没有找到相关文章