views:

349

answers:

5

Hello,

I want to refresh my asp.net page after someone clicks an "Add" button. However, the "Add" button is part of user control inside another user control and the child control and parent control are both wrapped in Update Panels: Code below is cut short for display, there's a reason the user control is inside another user control

Inside first control:

<ajax:UpdatePanel ID="Panel1" runat="server" UpdateMode="Always">
   <ContentTemplate>
             <uc:Control2 ID="Custom2" runat="server" />
   </ContentTemplate>
</ajax:UpdatePanel>

Then inside control2

<ajax:UpdatePanel ID="Panel2" runat="server" UpdateMode="Always">
   <ContentTemplate>
             <asp:LinkButton ID="AddButton" runat="server" OnClick="AddButton_Click"</asp:LinkButton>
   </ContentTemplate>
</ajax:UpdatePanel>
+2  A: 

If you want to reload the entire page (and not just the content of the UpdatePanels) you can do:

Page.Response.Redirect("mypagename.aspx");
Keltex
The page is dynamically generated. Can I do something like Response.Redirect(Page.Url) or something.
Yes. That would probably work. Response.Redirect(Page.Request.Url.AbsolutePath)
Keltex
A: 

Assuming you don't have control over the parent user control, you could find the control by searching through the child controls of the parent user control (FindControl). Then once you have the reference to it you could add an event handler:

btnAddButton.OnClick += new EventHandler(MethodThatRefreshesPage);

Since you're in update panels, you would need to do something like Response.Redirect(Request.RawUrl) to refresh the entire page.

Of course, if you do have control over the parent user control, you create your own "AddClicked" event and pass the subscriptions down to the link button..or you could just expose the LinkButton itself as a public property on the user control.

HTH

WayneC
A: 

I am assuming you want to refresh the page so the new added item is shown in the page... correct? if so, then you can databind your data control as part of the AddButton_Click event. For example, if you have a grid showing items, then a user clicks on the Add button to add a new item, you can put the following code in the AddButton_Click event to "refresh" the contents of the data control and display the newly added item:

protected void AddButton_Click(object sender, EventArgs e)
    {
       myGrid.DataBind();
    }

Hope this helps.

Ricardo
A: 

What about event bubbling (http://odetocode.com/code/94.aspx)?

Konstantin
A: 

You don't specify the configuration of your Script Manager, however assuming you have a Script Manager configured as follows:

<ajax:ScriptManager runat="server" ID="myScriptManager" EnablePartialRendering="true"></ajax:ScriptManager>

Then add this Trigger to the UpdatePanel template for Control2 so it looks as follows:

<ajax:UpdatePanel ID="Panel2" runat="server" UpdateMode="Always">
    <ContentTemplate>
        <asp:LinkButton ID="AddButton" runat="server" OnClick="AddButton_Click"</asp:LinkButton>
    </ContentTemplate>
    <Triggers>
         <asp:PostBackTrigger ControlID="AddButton" />
    </Triggers>
 </ajax:UpdatePanel>

This will trigger a PostBack that will allow you to reload any bound data (in Page_Load) and will also re-render the whole page and not just the contents of your inner update panel.

Simon Mark Smith