1

グリッドビューを更新しようとすると、必要なコントロールが見つかりません。コントロールは、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());
    }
4

3 に答える 3

1

コマンドフィールドの代わりに、

 <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)
{
}

CommandName="Update"を呼び出すことを忘れないでくださいLinkButton

于 2012-12-31T10:43:32.627 に答える
1
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.
}
于 2012-12-31T10:26:40.197 に答える
-1

多分?

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"); 
}
于 2016-07-29T11:48:51.920 に答える