10

I have an Updatepanel and Gridview inside it.

<asp:UpdatePanel ID="uplPanel" UpdateMode="Conditional" runat="server" OnLoad="uplPanel_Load">
<ContentTemplate>
 <asp:GridView ID="gvPrList" runat="server" AutoGenerateColumns="false" AllowPaging="false"
       AllowSorting="false" CssClass="list-table" HeaderStyle-CssClass="header">
       <Columns>
     <ItemTemplate>
               <asp:Button ID="btnEdit" runat="server" Text="Edit" CssClass="button save" OnCommand="onPrItemCmd"
                   CommandName="editRow" CommandArgument='<%#Bind("ID") %>' Style="width: 80px" />
               <asp:Button ID="btnDelete" runat="server" Text="Delete" CssClass="button save" OnCommand="onPrItemCmd"
                   CommandName="deleteRow" CommandArgument='<%#Bind("ID") %>' Style="width: 80px" />
           </ItemTemplate>
       </asp:TemplateField>
   </Columns>

When I click on my buttons inside the Griview, it does not fire the events. Any idea?

9 Answers 9

4

You need to add OnCommand event of GridView and then handle that inside that event like this:

OnRowCommand="gvPrList_OnRowCommand" 

or alternatively add a click event for the individual button and then handle in the code behind file:

<asp:Button ID="btnEdit" runat="server" OnClick="btnEdit_Click" Text="Edit" CssClass="button save"
                   OnCommand="onPrItemCmd" CommandName="editRow" CommandArgument='<%#Bind("ID") %>' Style="width: 80px" />
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I already have my code behind handler method for onPrItemCmd event.
Have you tried adding onclick event handler to your button and then checking if that works???
3

I had the same issue where column buttons with a OnClick were causing a postback but the OnClick method was not being hit. When I commented out the update panel and it all worked.

I resolved this issue by adding a postback trigger for the grid within the update panel:

</ContentTemplate>
   <Triggers>
       <asp:PostBackTrigger ControlID="uxWebDataGrid" />
   </Triggers>
</asp:UpdatePanel>

Hope this helps someone else!

Comments

2

I did the following and it works

I replace asp button with html button and call javascript method to fire Update Panal Load event.

<input id="btnDelete1" type="button" onclick="javascript:DeletePrItem('<%# Eval("ID") %>');" value="Delete" class="button save" style="width: 80px" />

My Js :

    function DeletePrItem(_Id) {
        __doPostBack('<%= uplPanel.ClientID %>', _Id);
    }

My Code behind :

    protected void uplPanel_Load(object sender, EventArgs e)
    {
        var arg = Request.Params.Get("__EVENTARGUMENT");

        if (arg != null)
        {
            if (arg != "")
            {
                string recordId = arg.ToString();
                //Do deletetion and rebind data grid

    }
     }
}

Comments

1

This would be the Event Handler for your command in the codebehind:

protected void onPrItemCmd(object sender, CommandEventArgs e)
    {
        //do whatever you want
        //probably you will need the "ID" or "CommandArgument":
        string myArgumentID = e.CommandArgument.ToString();

        uplPanel.Update(); //since the UpdatePanel is set *UpdateMode="Conditional"*
    }

UPDATE:

Probably, you might be doing some validation when you click on buttons. If so, you need to add CausesValidation="false" in your buttons or links properties

5 Comments

@Nilaa just wondering why are you using OnLoad="uplPanel_Load"? What are you doing in that procedure? This is a very straightforward task, and it is very weird it is not firing the events. It would be also a good idea to create the page from scratch and see where it breaks...
I have some another buttons which calls sharepoint model popup. using __doPostBack('<%= uplPanel.ClientID %>', ''); may be that cause me the issue. any idea?
I tried using this and get the exception The Update method can only be called on UpdatePanel with ID 'UpdatePanel1' before Render.
@vapcguy try replacing uplPanel with UpdatePanel1, which is your Update Panel's identifier...........
@aleafonso UpdatePanel1 is my identifier. uplPanel is the OP's identifier. Wouldn't've matter what I called it was what I was getting at- was just showing it was the same scenario, and you get the error when you call .Update() in a place .NET doesn't like (like the button click event). I managed to solve my issue in a way that has nothing to do with this post, though. I have a timer as a trigger that updates my panel, and my button click event enables my timer. I put my .DataBind() of a datatable to a gridview in my Timer1_Tick() function and it worked fine without .Update()
1

I had a similar issue.

Depending on your situation, as in mine...All of the clickable controls inside of the update panel I will want to be triggers; So i was simply able to use the UpdatePanel property 'ChildrenAsTriggers="true"' so solve the issue.

    <asp:UpdatePanel runat="server" ID="UPCommunicationlogForm" ChildrenAsTriggers="true" >

This solved my issue, now my edit and delete buttons that are generated from the ItemTemplate inside my gridview call their respective methods on the server.

Comments

0

Please add this code into the UpdatePanel.

</ContentTemplate> 
 <Triggers>
   <asp:PostBackTrigger ControlID="gvPrList" EventName="Click" />
 </Triggers>
 </asp:UpdatePanel>

4 Comments

I get this error : Type 'System.Web.UI.PostBackTrigger' does not have a public property named 'EventName'. I am using asp.net 3.5
protected void Click(object sender, CommandEventArgs e) { } //create this is a dummy event and try
'System.Web.UI.PostBackTrigger' does not have this property
If it's a PostBackTrigger, it doesn't need an event - only AsyncPostBackTrigger does.
0

I added an OnRowCommand Event and add this trigger to the UpdatePanel:

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="gvPrList" EventName="RowCommand" />
</Triggers>

Note that it's an Async trigger.

Comments

0

This helped me. In my case I was changing a value in a asp:DropDownList control and then going back to the server to update my asp:GridView which is in a different asp:UpdatePanel. I just forgot to add code to refresh the Update Panel with the list. Once I did that everything worked fine.

Server side

System.Data.DataSet ds = MySQL.DAL.GetRecord(sql);
GV_View.DataSource = ds;
GV_View.DataBind();
UP_MainList.Update(); //refresh the update panel

Aspx code

            <asp:UpdatePanel runat="server" ID="UP_ListChange" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:DropDownList runat="server" ID="ddFilter" DataTextField="Desc" AutoPostBack="true" DataValueField="Detail" OnSelectedIndexChanged="GV_CodeView_RefreshScreen" ></asp:DropDownList>
                </ContentTemplate>
             </asp:UpdatePanel>
            <div class="medium">Select Filter to view database codes</div>
        </div>
        <div class="div-row-header" runat="server" id="divList">
        <asp:UpdatePanel runat="server" ID="UP_MainList" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:GridView ID="GV_View" runat="server" AllowSorting="True" AutoGenerateColumns="false" DataKeyNames="id">
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Id" Visible="false" />
                    <asp:BoundField DataField="Type_Cd" HeaderText="Code" />
                    <asp:BoundField DataField="Short_Desc" HeaderText=" Description" />
                    <asp:TemplateField HeaderText="">
                        <ItemTemplate>
                            <asp:Button ID="Code_Edit" runat="server" Text="Edit" onClick="GV_Code_Edit_Click" class="button" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>             
        </ContentTemplate>

        </asp:UpdatePanel>
        </div>

Comments

0

It should work as expected, but I forgot that you don't get an error message like normal. In my case the update command was invalid, and then it seems that the update button doesn't work.

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.