Search code examples
c#asp.netgridviewupdatepanelasplinkbutton

Why LinkButton inside GridView which is present in Updatepanel not firing OnClientClick event?



I have placed my GridView inside the Updatepanel and there I have defined few columns with one LinkButton. But for that LinkButton OnClientClick event is not firing. Instead it's doing a postback.
Following is the Code:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
   <ContentTemplate>
      <asp:GridView ID="gvUnmappedICD" runat="server" EmptyDataText="No Records are added yet."
             OnRowCommand="gvUnmappedICD_RowCommand" OnRowDataBound="gvUnmappedICD_RowDataBound">
          <Columns>
             <asp:TemplateField HeaderText="Action">
                 <ItemTemplate>
                    <asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%#Eval("KEYWORD") %>' CommandName="remove" ClientIDMode="AutoID"
                        OnDataBinding="lnkRemove_DataBinding" OnClientClick='return confirm("Are you sure you want to Delete this?");' ToolTip="Click to Remove this record." Text="Remove" />
                 </ItemTemplate>
          </Columns>
      </asp:GridView>
   </ContentTemplate>
</asp:UpdatePanel>

This LinkButton with the ID lnkRemove should display a confirm message box when user clicks on it. But it's not showing it.
I have tried registering the Asynchronous PostBack event to this from code behind as follows:

ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lbRemove);

Kindly anyone help me to resolve this.


Solution

  • Use a PostBackTrigger

    <asp:ScriptManager ID="scriptManager" runat="server">
            <asp:UpdatePanel ID="updatePanel" runat="server">
                <asp:GridView ID="gvUnmappedICD" runat="server" EmptyDataText="No Records are added yet."
             OnRowCommand="gvUnmappedICD_RowCommand" OnRowDataBound="gvUnmappedICD_RowDataBound">
          <Columns>
             <asp:TemplateField HeaderText="Action">
                 <ItemTemplate>
                    <asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%#Eval("KEYWORD") %>' CommandName="remove" ClientIDMode="AutoID"
                        OnDataBinding="lnkRemove_DataBinding" OnClientClick='return confirm("Are you sure you want to Delete this?");' ToolTip="Click to Remove this record." Text="Remove" />
                 </ItemTemplate>
          </Columns>
      </asp:GridView>
                <Triggers>
                    <asp:PostBackTrigger ControlID="lnkRemove" />
                </Triggers>
           </asp:UpdatePanel>