editTemplate findControl in gridView_RowCreated Event

Posted on 2011-10-07
Medium Priority
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
  • 2
LVL 16

Accepted Solution

Easwaran Paramasivam earned 2000 total points
ID: 36930569
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
ID: 36930586

Author Closing Comment

ID: 36943847
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!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

569 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