Rida Iftikhar
Rida Iftikhar

Reputation: 186

FindControl returns null in OnRowCommand Event

I have a gridview with few controls in it and I'm trying to get these controls in OnRowCommand event by using the FindControl method, but it always returns null.

This is the 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>

And here is the row command event

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

what I'm trying to do is get the selected row so and values from its controls, so that I can use them in Transaction page.

Edit: The FindControl method is working fine in RowUpdating and RowDataBound events

Upvotes: 1

Views: 1134

Answers (2)

aditya srivastava
aditya srivastava

Reputation: 723

this may help you out

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName.Equals("CompleteTransaction"))
        {                
           int rowIndex = Convert.ToInt32(e.CommandArgument); // Get the current row 

            GridViewRow row = GridView1.Rows[index];
            Label lblName = (Label)row.FindControl("lblName")
            DropDownList drpList= (DropDownList)row.FindControl("ddlServiceType");
            lblName.Text = drpList.SelectedValue;             
        }
    }

Upvotes: 2

VDWWD
VDWWD

Reputation: 35564

Since your DropDownLists is in the EditItemTemplate, you need to use the EditIndex to get the correct row.

DropDownList ddlServiceType = (DropDownList)GridView1.Rows[GridView1.EditIndex].FindControl("ddlServiceType");
ddlServiceType.BackColor = Color.Red;

Upvotes: 2

Related Questions