Solved

ASP.NET Gridview control does not fire rowcommand event inside an ajax toolkit's update panel

Posted on 2009-05-17
2
6,813 Views
Last Modified: 2012-05-07
Hi, I've been browsing google and ex from last several hours but could not find a resolution of my problem. The problem is that when i place a gridview control inside an ajax toolkit update panel, the rowcommand event is not fired. As it seems the callback event is being raised but the code inside the grid view rowcommand event handler is never reached. I've attached my code with this post, in which i've placed a gridview and a form with a text box inside an undate panel. I need to set textbox value fetching from database based upon the imagebutton click which is having command name and command argument attributes.
================== Design Source ==================
<asp:UpdatePanel ID="pnlMain" runat="server" UpdateMode="Always">
    <ContentTemplate>
        <table class="entryform" cellpadding="0" cellspacing="0" width="95%">
            <tr>
                <td class="first" style="width: 172px; height: 24px; vertical-align:top;">
                    Current Categories:</td>
                <td class="last" style="height: 24px">
                    <asp:GridView ID="gvwData" DataKeyNames="CategoryId" CssClass="grid" runat="server" Width="100%" AutoGenerateColumns="false"
                        AllowPaging="True" PagerSettings-Mode="Numeric" onpageindexchanging="gvwData_PageIndexChanging" PageSize="10" 
                        OnRowCommand="gvwData_RowCommand" OnRowCreated="gvwData_RowCreated" OnRowDeleting="gvwData_RowDeleting" OnRowEditing="gvwData_RowEditing">
                        <EmptyDataTemplate>
                            <span style="color:White;">No Category Found.</span>
                        </EmptyDataTemplate>
                        <EmptyDataRowStyle CssClass="emptyrow" />            
                        <RowStyle CssClass="row" />
                        <AlternatingRowStyle CssClass="altrow" />
                        <Columns>
                            <asp:BoundField DataField="CategoryId" ReadOnly="True" HeaderText="ID" Visible="False" />
                            <asp:BoundField DataField="Name" ReadOnly="True" HeaderText="Name" />
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:ImageButton ID="btnDetails" CausesValidation="false" runat="server" ImageUrl="~/assets/images/admin/edit.gif"
                                        CommandArgument='<%# Eval("CategoryId")  %>' CommandName="Details" ToolTip="View / Edit" />
                                    <asp:ImageButton ID="btnDelete" CausesValidation="false" runat="server" ImageUrl="~/assets/images/admin/del.gif"
                                        CommandArgument='<%# Eval("CategoryId")  %>' CommandName="Delete" ToolTip="Delete"
                                        OnClientClick="return confirm('Do You Really Want To Delete This Category?')"/>                    
                                </ItemTemplate>
                                <ItemStyle Width="50px" HorizontalAlign="Center" />
                            </asp:TemplateField>
                        </Columns>
                        <PagerStyle CssClass="pager"/>
                    </asp:GridView>
                </td>
            </tr>
            <tr>
            <td class="first">
                    Name:</td>
            <td class="last">
                    <asp:TextBox ID="txtName" runat="server" Width="300px" MaxLength="255"></asp:TextBox>
            </td>
            </tr>
            <tr class="fullrow">
	<td colspan="2"></td>
            </tr>
            <tr class="bg">
	<td class="first"></td>
	<td class="last">
	<asp:Button ID="btnSave" runat="server" onclick="btnSave_Click" Text="Save" 
                        Width="60px" />
                    <asp:Button ID="btnCancel" runat="server" CausesValidation="False" onclick="btnCancel_Click" 
                        Text="Cancel" Width="60px" />
	            </td>
            </tr>
        </table>
    </ContentTemplate>
</asp:UpdatePanel>
 
================== Code Behind ==================
protected void BindData()
        {
            gvwData.DataSource = datasource; // datasource is a dataset fetched from database
            gvwData.DataBind();
        }
 
        protected void SetEditable(int cid)
        {
            Category cat = new Category();
            if (cat.IsLoaded)
            {
                txtName.Text = cat.Name;
                btnSave.Text = "Update";
            }       }
 
        protected void gvwData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Details")
            {
                SetEditable(int.Parse(e.CommandArgument.ToString()));
            }
            else if (e.CommandName == "Delete")
            {
                Category cat = new Category(int.Parse(e.CommandArgument.ToString()));
                if (cat.IsLoaded)
                {
                    cat.IsDeleted = true;
                    cat.Save(Context.User.Identity.Name);
 
                    BindData();
                }
            }
        }

Open in new window

0
Comment
Question by:23-degrees
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 12

Accepted Solution

by:
techExtreme earned 250 total points
ID: 24406220
Hi,
Please verify following CheckList:

1) You have not posted your page load event handler. Please check it that when you are binding the grid for the first time, you are doint it inside the 'Not' postback , i.e. if page is not postback -> bind the grid.  Check it out.

2) Is your row command event fired if you remove ajax and allow full page postback?

I guess we'll be able to find the solution once these checks are made.
0
 
LVL 12

Expert Comment

by:techExtreme
ID: 24406227
err I forgot to mention the 3rd step,

3) Ensure there are no exceptions while grid is bound, to check it , put a try catch around inside when you call grid's .databind() event. There should ideally no exceptions when grid is bound, else we'll have problems with other events through the page lifecycle.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

624 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