4

I'm having an issue with trying to add a button to my grid. My GridView is first loaded with data in the PageLoad event.

I'm then taking the data in the first cell of each row, and creating a button that will link to a URL. To get the URL, I have to run a query with the data in the first cell as a parameter. I was doing this in the RowDataBound event at first, but hitting that query for every row was making it really slow.

So I decided to add a button that would retrieve the URL only when you clicked the button.

Here's my GridView:

<asp:GridView ID="gvResults" runat="server"
    OnRowDataBound="gvResults_RowDataBound"
    OnRowCommand="gvResults_RowCommand">               
</asp:GridView>    

And my code:

protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.DataItem != null)
    {
        LinkButton lb = new LinkButton();
        lb.CommandArgument = e.Row.Cells[0].Text;
        lb.CommandName = "NumClick";
        lb.Text = e.Row.Cells[0].Text;

        e.Row.Cells[0].Controls.Add((Control)lb);
    }
}


protected void gvResults_RowCommand(object sender, CommandEventArgs e)
{
    switch (e.CommandName.ToLower())
    {
        case "numclick":
            string url = GetUrl(e.CommandArgument.ToString());
            Response.Redirect(url);
            break;
        default:
            break;
    }
}

The grid generates fine, the button gets added to the grid for each row. But when I click on it, the RowCommand event doesn't fire, and the page just refreshes.

Does anyone know what the issue is?

1
  • 1
    I would associate an ID to the linkButton before adding it to the Cell. Commented Jan 19, 2011 at 16:01

3 Answers 3

3

Why use a dynamic button at all? You can easily put the linkbutton directly into the markup of the gridview (as long as you don't mind using a template field) and there will be no need to mess around with the RowDataBound event.

Your markup would look something like the following:

<Columns>
    <asp:TemplateField HeaderText="SomeHeaderText">
            <ItemTemplate>
                <asp:LinkButton ID="lnkBtn" runat="server" CommandName="NumClick" CommandArgument= '<%# (string)Eval("dbValue") %>'  Text='<%# (string)Eval("dbValue") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>        
    <asp:BoundField></asp:BoundField>
    <asp:BoundField></asp:BoundField>
    <asp:BoundField></asp:BoundField>
</Columns>

Add breakpoints to the RowCommand event and make sure that you can hit the breakpoints.

The problem may lie elsewhere.

Also, make sure that you're not databinding on postback.

Sign up to request clarification or add additional context in comments.

3 Comments

That's the problem, I did add a breakpoint to the RowCommand event, and the event never fired. And I'm not databinding on postback.
Awesome. It's always good practice to try and use markup as much as possible. If something can be moved out of your code behind to markup then go for it.
if i have the grid view which is dynamically populated.. then i cannot declare the fields in the markup because the no.of columns may change every time. then how should we go about it..
1

You have a big trouble with your code. It's pretty hard for me to explain what's your big mistake, but I can easily tell you how to fix.

The problem is that you generate a new button inside the RowDataBound event, definitely the wrongest choice. The button gets rendered because it exists after that event when page renders, but doesn't exist before data binding. If you bind data everytime you load the page (even during postback) the button still gets rendered because you generate a new button.

But since the button doesn't exist before data binding, it cannot raise events. You must declare the button from markup into a template of GridView, then access it not by using new LinkButton() but by using e.Row.Cells[0].FindControl("buttonId") and set its text. Then, you have to set its markup in order to fire its own Command event (not RowCommand) and handle it as you used (don't forget to set CommandArgument during data binding)

[Edit] I also made a mistake: controls inside data bound controls also don't exist before data binding. But they are initialized not with new Control() (by the private methods of data bound control) but with Page.LoadControl(typeof(Control)). That's the first thing you must fix when you load controls dynamically!!

1 Comment

I also have almost the same problem.I have a dynamic grid in which I am attaching two events.Rowcommand and rowdatabound.In row databound event i am adding some edit and delete button. Can i add those edit and delete button on each row in somewhere else
0

Because the control is added dynamically on databind and you have to databind the gridview for each postback, the control being "clicked" is different each time. The event doesn't fire because at the time it needs to fire it doesn't exist as it did in the last iteration of the page.

I notice you don't have any logic determine if the button should be there, and it always goes into cell[0].

You should place this button into a TemplateItem so that it exists properly. If you have a need to do it in code-behind, you are probably better served doing it in the RowCreated event.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.