[c#]FindControl 返回 null 在 OnRowCommand Event 中

标签: Asp.net GridView C#
发布时间: 2017/2/25 3:53:29
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有几个控件与 gridview 里面的和我想要这些控件在 OnRowCommand 事件中通过使用调用,但它始终返回 null。

这是 gridview

<asp:GridView ID="GridView1" runat="server" CssClass="table table-striped table-bordered" AutoGenerateColumns="false" DataKeyNames="ID" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"
    OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating">
    <Columns>
        <asp:TemplateField HeaderText="Service Type">
            <ItemTemplate>
                <asp:Label Text='<%# Eval("Fund_Service_Type") %>' runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlServiceType" runat="server">
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Method">
            <ItemTemplate>
                <asp:Label Text='<%# Eval("Fund_Method") %>' runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlFundMethod" runat="server">
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Source">
            <ItemTemplate>
                <asp:Label Text='<%# Eval("Fund_Source") %>' runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="ddlFundSource" runat="server">
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Amount">
            <ItemTemplate>
                <asp:Label Text='<%# Eval("Fund_Amount") %>' runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtAmount" runat="server" Text='<%# Eval("Fund_Amount")%>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField Visible="true" HeaderText="" ShowHeader="false">
            <ItemTemplate>
                <asp:LinkButton ID="btnRedirect" runat="server" CommandArgument='<%# Bind("ID") %>' CommandName="CompleteTransaction" Text="Complete Transaction"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这里是行命令事件

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName.Equals("CompleteTransaction"))
        {                
            GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            int rowIndex = Convert.ToInt32(e.CommandArgument); // Get the current row
            DropDownList ddlServiceType = (DropDownList)GridView1.Rows[rowIndex].Cells[0].FindControl("ddlServiceType");//retuns null

            GridViewRow selectedRow = GridView1.Rows[rowIndex];
            DropDownList name = (DropDownList)gvr.Cells[0].FindControl("ddlServiceType"); //also returns null
            Server.Transfer("~/Transaction.aspx");
        }
    }

我试图做所以是获取所选的行和值从其控件,以便我可以在交易页面中使用它们。

编辑︰调用在 RowUpdating 和 RowDataBound 事件中运行良好

解决方法 1:

因为你的 DropDownLists 是在 EditItemTemplate ,您需要使用 EditIndex 来获得正确的行。

DropDownList ddlServiceType = (DropDownList)GridView1.Rows[GridView1.EditIndex].FindControl("ddlServiceType");
ddlServiceType.BackColor = Color.Red;
官方微信
官方QQ群
31647020