1

i created a web application wherein i used the onrowcommand and created the handler in code behind, now i have a button inside the emptytemplate, whenever i click the button, my onRowCommand is not executed. Below is my code.

<asp:GridView ID="grdExternalLinkSection1" runat="server" Width="100%" AutoGenerateColumns="false" CellPadding="5" OnRowCommand="grdExternalLinkSection_RowCommand">
                                <EmptyDataTemplate>
                                    External Link Title
                                    <asp:TextBox ID="txtExternalLinkTitleEmptySection1" runat="server"></asp:TextBox>
                                    External Link Url
                                    <asp:TextBox ID="txtExternalLinkUrlEmptySection1" runat="server"></asp:TextBox>
                                    <asp:Button ID="btnExternalLinkEmptySection1" runat="server" Text="Add" CommandArgument="1" CommandName="headernew" style="padding:3px; width:56px;" />
                                </EmptyDataTemplate>
</asp:GridView>

there are more fields but this is what i am talking about. and here is my code behind handler for the RowCommand Event.

protected void grdExternalLinkSection_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Response.Write("welcome");
    }

it never excutes the handler, and below is my page directive:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="NewsletterASPVersion.ascx.cs" Inherits="RWO_Controls_NewsletterASPVersion" %>

this worked once, and after thereafter never working. does anyone have any idea what could be causing this.

Abbas
  • 4,948
  • 31
  • 95
  • 161

1 Answers1

4

There are two possible traps that you might have fallen into:

  1. You are rebinding the GridView to it's DataSource on every postback. So always check:

    if(!IsPostBack)BindGrid();
    
  2. You are not calling grdExternalLinkSection1.DataBind() when the DataSource is empty

But then you would not see the EmptyDataTemplate at all. So i guess that you've fallen into the first trap.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • thanks !IsPostback came out to be the problem, actually i have wrote the binding code outside the !IsPostback and directly inside the Page_Load, can you tell whats the difference between writing code directly inside page_load and under !IsPostback. – Abbas Mar 07 '12 at 16:19
  • 1
    @Abbas: The difference is that page_load is called everytime the page posts back to the server so not only the first time. The `IsPostBack`-check ensures that this part is only called for the very first time. That is required, otherwise all changes that the user has made(f.e. when GridView is in edit-mode)are lost and events(your issue) are not triggered. The reason behind is that calling `GridView.DataBind()` causes the grid to recreate all of the controls. – Tim Schmelter Mar 07 '12 at 16:23
  • but in that case, if there is a button inside the gridview, and when that button is clicked, other grids binding will not happen, as i am placing the grid bindings in !IsPostback block, and in this case there is a postback – Abbas Mar 07 '12 at 16:41
  • 1
    @Abbas: If you need to rebind the GridView on postbacks(f.e. because something in the DataSource was changed or due to Sorting/Paging), you need to call your databind-method that does it from the appropriate event-handler(f.e. the PageIndexChanged-Event or your button's click-event) and **not** from page_load! – Tim Schmelter Mar 07 '12 at 17:17