ptownbro
ptownbro

Reputation: 1284

Button Inside Gridview Inside Update Panel Not Working

I'm just starting to use Update Panels in my solutions, so I'm slowly getting familiar with them. I have a GridView that has a delete button associated with each row displayed. When I click on the delete button, it's OnClick event should displays a panel which acts as semi-modal confirmation box (done as a lightbox) to delete the record associated with the relevant row. However, when I click the button, the panel doesn't show because all of it is in a Update Panel. Works fine without the update panel

Any ideas?

Here's a stripped down version of my code:

<script runat="server">
Protected Sub LinkButtonDelete_Click(ByVal Sender As Object, ByVal e As EventArgs)
    PanelConfirmMessage.Visible = True
    PanelConfirmLightBox.Visible = True
End Sub
'.... note there is other code that handles the delete ...
</script>

<html>
<head"></head>
<body>
<form id="form1" runat="server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" DataSourceID="DataSourceGridView1">
                <Columns>
                    <asp:templatefield HeaderText="Name">
                        <ItemTemplate><asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("MyFieldName") %>' /></ItemTemplate>
                    </asp:templatefield>
                    <asp:TemplateField HeaderText="Delete">
                        <ItemTemplate><asp:LinkButton ID="LinkButtonDelete" runat="server" OnClick="LinkButtonDelete_Click"></asp:LinkButton></ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="DataSourceGridView1" runat="server" ConnectionString="<%$ ConnectionStrings:MyString %>" SelectCommand="MySelectCommand" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Panel runat="server" ID="PanelConfirmMessage" Visible="false">
        <p>Are you sure you want to continue?</p>
        <asp:Button runat="server" ID="ButtonYes" Text="Yes" OnClick="ButtonYes_Click" />
        <asp:Button runat="server" ID="ButtonNo" Text="No" OnClick="ButtonNo_Click" />
    </asp:Panel>
    <asp:Panel runat="server" id="PanelConfirmLightBox" Visible="false"></asp:Panel>
</form>
</body>
</html>

Upvotes: 1

Views: 3333

Answers (2)

Rojalin Sahoo
Rojalin Sahoo

Reputation: 1025

To post back your gridview template field control from update panel you can add the control to the Trigger collection in the code behind on the DataBind event of the item.

So try add this OnDataBinding of LinkButton :

   protected void PostBackBind_DataBinding(object sender, EventArgs e)
{
   LinkButton lb = (LinkButton) sender;
   ScriptManager sm = (ScriptManager)Page.FindControl("scriptmanagerId");
   sm.RegisterPostBackControl(lb);
}



<asp:ScriptManager ID="scriptmanagerId" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server">
                <Columns>
                    <asp:templatefield HeaderText="Name">
                        <ItemTemplate><asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("name") %>' /></ItemTemplate>
                    </asp:templatefield>
                    <asp:TemplateField HeaderText="Delete">
                        <ItemTemplate><asp:LinkButton ID="LinkButtonDelete" runat="server" Text="Delete" OnClick="LinkButtonDelete_Click" OnDataBinding="LinkButtonDelete_DataBinding"></asp:LinkButton></ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>

Upvotes: 0

ptownbro
ptownbro

Reputation: 1284

Thanks all for you help. I was provided an answer through the help of someone else. The answer was simple. All I had to do was place the confirmation inside the update panel along with everything else.

Shortened version with fix:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:GridView ID="GridView1"...>
            <Columns>....</Columns>
        </asp:GridView>
        <asp:SqlDataSource ...></asp:SqlDataSource>

        <!--- THE CONFIRMATION PANELS INSIDE THE UPDATEPANEL AS SHOWN HERE --->

        <asp:Panel runat="server" ID="PanelConfirmMessage" Visible="false">
        ...
        </asp:Panel>
        <asp:Panel runat="server" id="PanelConfirmLightBox" Visible="false"></asp:Panel>
    </ContentTemplate>
</asp:UpdatePanel>

Upvotes: 0

Related Questions