4
votes

I have an asp page with a regular panel that contains a gridview. The gridview has some linkbuttons that are used to generate a pdf.

I also have an UpdatePanel inside the regular panel and below the gridview that contains a label.

When the user clicks the 'generate' linkbutton inside the gridview, I want the label to say 'report generating' or something like that.

I have been unable to get this to work at all. I can throw values into the label all day long and nothing.

Here is the code for the panel:

<asp:Panel ID="ReportsPanel" runat="server" Height="611px">
    <h2>
        Reports
    </h2>
    <asp:Label ID="ReportsNotification" runat="server"></asp:Label>
    <br />
    <asp:Label ID="Label1" runat="server" Text="Pharmacy: "></asp:Label>
    <asp:DropDownList ID="PharmaciesDropDownList" runat="server" AutoPostBack="true"
        OnSelectedIndexChanged="PharmaciesDropDownList_SelectedIndexChanged">
    </asp:DropDownList>
    <br />
    <br />
    <asp:GridView ID="ListReportsGridView" runat="server" AllowPaging="true" AllowSorting="true"
        GridLines="None" PageSize="10" CssClass="GridView" AutoGenerateColumns="false"
        EmptyDataText="No rows for selection." OnRowCommand="ListReportsGridView_RowCommand"
        OnPageIndexChanging="ListReportsGridView_PageIndexChanging"
        EnablePersistedSelection="True"
        DataKeyNames="FinDataID">
        <AlternatingRowStyle CssClass="GridViewAltRow" />
        <HeaderStyle CssClass="GridViewHeader" />
        <RowStyle CssClass="GridViewItem" />
        <PagerStyle CssClass="GridViewPager" />
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" ControlStyle-CssClass="Hidden" ItemStyle-CssClass="Hidden"
                HeaderStyle-CssClass="Hidden" FooterStyle-CssClass="Hidden" />
            <asp:BoundField DataField="FinDataID" HeaderText="FinDataID" ControlStyle-CssClass="Hidden" ItemStyle-CssClass="Hidden"
                HeaderStyle-CssClass="Hidden" FooterStyle-CssClass="Hidden" />
            <asp:BoundField DataField="PeriodStart" HeaderText="Period Start" DataFormatString="{0:dd/MM/yyyy}" />
            <asp:BoundField DataField="PeriodEnd" HeaderText="Period End" DataFormatString="{0:dd/MM/yyyy}" />
            <asp:TemplateField HeaderText="Reports Available for Download"><ItemTemplate><asp:LinkButton ID="LinkButtonViewRpt" Text='<%#Eval("FileName") %>' CommandName="ViewRpt" CommandArgument="<%# Container.DataItemIndex %>" runat="server"></asp:LinkButton></ItemTemplate></asp:TemplateField>    
            <asp:TemplateField Visible="false"><ItemTemplate><asp:LinkButton ID="LinkButtonGenerateRpt" Text='Generate' CommandName="GenerateRpt" CommandArgument="<%# Container.DataItemIndex %>" runat="server"></asp:LinkButton></ItemTemplate></asp:TemplateField>    
            <asp:TemplateField Visible="false"><ItemTemplate><asp:LinkButton ID="LinkButtonUploadRpt" Text='Upload' CommandName="UploadRpt" CommandArgument="<%# Container.DataItemIndex %>" runat="server"></asp:LinkButton></ItemTemplate></asp:TemplateField>    
        </Columns>
    </asp:GridView>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="LinkButtonGenerateRpt" EventName="Click" />
        </Triggers>
        <ContentTemplate>
            <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
    <br />
</asp:Panel>

I've tried with a button in the UpdatePanel, outside the UpdatePanel but neither of those ideas worked either. Surely this is possible!

The one thing I'm wondering, is when the 'generate' button fires and creates the pdf it is calling Response.End. I know this will end the thread and not reload the page. But I was hoping I could refresh the UpdatePanel before this happens... from what I can tell that's what UpdatePanel's are for, partial page updates.

Here is the code behind for the gridview:

protected void ListReportsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    intIndex = Convert.ToInt32(e.CommandArgument);
    if (e.CommandName == "GenerateRpt")
    {//render pdf

        //populate empty report object with row information
        objFileData = new FileData();
        objFileData.PharmacyID = Convert.ToInt32(PharmaciesDropDownList.SelectedValue);
        objFileData.FinDataID = Convert.ToInt32(ListReportsGridView.Rows[intIndex].Cells[1].Text);
        objFileData.CreateDate = Convert.ToDateTime(DateTime.Now);
        objFileData.CreatedByID = Convert.ToInt32(Session["UserID"]);
        objFileData.PeriodStart = Convert.ToDateTime(ListReportsGridView.Rows[intIndex].Cells[2].Text);

        lblMessage.Text = "Report generating...";
        //btnGenerate_Click(sender, e);
        RenderPdf();

    }

Thanks in advance!

EDIT: And I'm also receiving this error...

A control with ID 'LinkButtonGenerateRpt' could not be found for the trigger in UpdatePanel 'UpdatePanel1'.

Do I need to use the UniqueID or is scope otherwise an issue?

2
added a comment under the answer I added - Kiarash

2 Answers

4
votes

Your updatepanel UpdateMode is conditional and you have to trigger the update yourself so when you need to update it you have to say UpdatePanel1.Update();

3
votes

You cannot set a button that is in a different naming container with the updatepanel as a trigger. This is the reason why you get the error:A control with ID 'LinkButtonGenerateRpt' could not be found for the trigger in UpdatePanel 'UpdatePanel1

If you really want the Linkbutton to be a trigger, you need to use ScriptManager.RegisterAsyncPostBackControl method, even Programmatically adding AsyncPostBackTrigger controls is NOT supported.

You could handle the GridView's OnRowCreated event and call ScriptManager.RegisterAsyncPostBackControl for each LinkButton.Find out the control via findcontrol method. Then in the linkbutton's OnClick event handler call UpdatePanel.Update() to get it to update.

GridView Markup ( only Linkbutton shown below ). Define an OnClick event for your link button.

<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:LinkButton ID="LinkButtonGenerateRpt" Text='Generate' 
     CommandName="GenerateRpt" 
     CommandArgument="<%# Container.DataItemIndex %>" 
     OnClick="LinkButtonGenerateRpt_Click"runat="server">
    </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

// Grid View's RowCreated event. Here you Register the linkbuttons as asyncpostback triggers

protected void ListReportsGridView_RowCreated(object sender, GridViewRowEventArgs e)
 {
    Control lnkBtnControl = e.Row.Cells[5].FindControl("LinkButtonGenerateRpt");
       if (lnkBtnControl!= null)
         {
            ScriptManager1.RegisterAsyncPostBackControl(lnkBtnControl );
         }    
 }

And update the UpdatePanel on LinkButton click as:

protected void LinkButtonGenerateRpt_Click(object sender, EventArgs e)
        {

            UpdatePanel1.Update();

        }