editTemplate findControl in gridView_RowCreated Event

Posted on 2011-10-07
Last Modified: 2012-06-27
I'm trying to set the .CommandArgument to the string representation of the RowIndex.

When I use FindControl, the ItemTemplate fields work fine, but the EditTemplate fields return null.

Can anyone offer some assistance to resolve this?

Ps, I'm using Visual Studio 2010 Professional, and ASP.NET 4.0 .NET Framework.
<asp:GridView ID="gvYourOpenJobs" 
                              EmptyDataText="No Jobs Found" onrowdatabound="gvYourOpenJobs_RowDataBound" onrowcreated="gvYourOpenJobs_RowCreated" AllowSorting="True" OnRowUpdating="gvYourOpenJobs_RowUpdating" AllowPaging="True">
                     <AlternatingRowStyle BackColor="#E9ECF1" ForeColor="RoyalBlue" />
                    <asp:TemplateField ShowHeader="False">
                          <asp:LinkButton ID="ButtonApprove" runat="server" CausesValidation="True" CommandName="Update" ForeColor="#009933" Text="Approve"></asp:LinkButton>
                          <asp:LinkButton ID="lBtnDeny" runat="server" CausesValidation="False" CommandName="Deny" ForeColor="#009933" Text="Deny"></asp:LinkButton>
                          <asp:LinkButton ID="lBtnCancel" runat="server" CausesValidation="False" CommandName="Cancel" ForeColor="Red" Text="Cancel"></asp:LinkButton>
                        <asp:LinkButton ID="lBtnApproveDeclineJob" runat="server" CausesValidation="False" CommandName="Edit" ForeColor="RoyalBlue" Text="Approve or Deny"></asp:LinkButton>

    protected void gvYourOpenJobs_RowCreated(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            LinkButton selectButton = (LinkButton)e.Row.FindControl("btnYourOpenJobsSelect");
            if (selectButton != null)
                selectButton.CommandArgument = e.Row.RowIndex.ToString();

            LinkButton lBtnApproveDeclineJob = (LinkButton)e.Row.FindControl("lBtnApproveDeclineJob"); // ok
            if (lBtnApproveDeclineJob != null)
                lBtnApproveDeclineJob.CommandArgument = e.Row.RowIndex.ToString();

            LinkButton approveJob = e.Row.Cells[0].FindControl("ButtonApprove") as LinkButton; // returns null
            if (approveJob != null)
                approveJob.CommandArgument = e.Row.RowIndex.ToString();

            LinkButton denyJob = (LinkButton)e.Row.FindControl("lBtnDeny"); // returns null
            if (denyJob != null)
                denyJob.CommandArgument = e.Row.RowIndex.ToString();

            LinkButton cancelJob = e.Row.Cells[0].FindControl("lBtnCancel") as LinkButton; // returns null
            if (cancelJob != null)
                cancelJob.CommandArgument = e.Row.RowIndex.ToString();

Open in new window

Question by:jackwebb22002
    LVL 16

    Accepted Solution

    RowDataBound is the best place to find the controls.

    protected void gvYourOpenJobs_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow && 
            (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
            // Find control
            LinkButton dl = (DropDownList)e.Row.FindControl("ButtonApprove");

    Open in new window

    LVL 16

    Expert Comment

    by:Easwaran Paramasivam

    Author Closing Comment

    Yes, I remember that from last year when i initially built this application.  Sorry, but info in my brain has an expiration timer of about an hour!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now