3

I have an UpdatePanel in my default.aspx page and the UpdatePanel has asp placeholder, also I have an ascx control that is the navigation of the site and it is created dynamically based on the data in the database, each navigation item is an ImageButton, and my each loop in DataList has HiddenField value of the URL for each corresponded ascx control like Value="~/controls/somecontrol.ascx"

Here is what I want/need to do: I need to create triggers dynamically for my UpdatePanel that is in default.aspx in my ascx navigation control, so what I am exactly looking to do is that my each navigation item that is an "ImageButton" to be a trigger for this UpdatePanel and when you click on it, it will reference the placeholder in UpdatePanel and load the control based on the NavigateUrl path and do placeholder.Controls.Add(mycontrol).

Default.aspx page:

 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:PlaceHolder ID="phMainHolder" runat="server"></asp:PlaceHolder>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

Navigation ascx control:

<asp:DataList ID="dlnavigations" runat="server"  RepeatDirection="Horizontal" 
                onitemcommand="dlnavigations_ItemCommand" OnItemDataBound="dlnavigations_ItemDataBound">
                <ItemTemplate>                      
                    <asp:HiddenField ID="hfURL" Value='<%#Eval("strUrl")%>' runat="server" />     

                        <asp:ImageButton ID="imgTab" Width="20"   CommandArgument='<%#Eval("ID")%>'  
                            ImageUrl='<%#Eval("strIcon")%>' runat="server" />                        
                </ItemTemplate>
            </asp:DataList>


protected void dlnavigations_ItemCommand(object source, DataListCommandEventArgs e)
            {
                HiddenField hfURL = (HiddenField)e.Item.FindControl("hfURL");
                Control ctrl = LoadControl(hfURL.Value);
                myplaceholderinUpdatePanel.Controls.Clear();
                //here i need to reference my placeholder in UpdatePanel then
                myplaceholderinUpdatePanel.Controls.Add(ctrl);

            }

I am not very sure if this is really possible to do with UpdatePanel please any help appropriated, if i cant do it with UpdatePanel are there any other way of doing this same concept?

thank.

Nazo Tajrian
  • 131
  • 2
  • 14
  • Hi Tim, sorry i am not fimiler with the concept you are telling me to do, how can I call Update method programmatically from the LinkButton's click? do you have any sample code or know any post dose the same thing? because I spent lot of time searching on this but couldn't find any solution! – Nazo Tajrian Mar 12 '12 at 21:26
  • Sorry, i've just deleted that comment since i've made it [an answer](http://stackoverflow.com/a/9675045/284240). – Tim Schmelter Mar 12 '12 at 21:29
  • I'm not sure if it matches your requirement since you've edited your question and added more details, but hopefully you understand my approach better now(see my edit). – Tim Schmelter Mar 12 '12 at 22:16

1 Answers1

5

If i've understood your requirement correctly i would suggest another approach:

Set the UpdatePanel1's UpdateMode property to Conditional

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:PlaceHolder ID="phMainHolder" runat="server"></asp:PlaceHolder>
    </ContentTemplate>
</asp:UpdatePanel>

To clarify the event driven approach a bit, assume this is your UserControl's codebehind:

public partial class Navigation : System.Web.UI.UserControl
{
   public delegate void NavigationHandler(int ID);
    public event NavigationHandler Navigated;

    void LinkButton_Command(Object sender, CommandEventArgs e)
    {
        int ID=int.Parse(e.CommandArgument.ToString());
        Navigated(ID);
    }
}

And your page can handle this event in the following way:

protected void Page_Init(object sender, EventArgs e) {
    this.Navigation1.Navigated += UserNavigated;
}

protected void UserNavigated(int ID){
    //do whatever you need to do and then call...
    UpdatePanel1.Update();
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I will try this but isn't you think going to postback the page again on each LinkButton_Command? because the whole reason for this I want to use the UpdatePanel to not postback the whole page. – Nazo Tajrian Mar 12 '12 at 22:39
  • by the way, it doesn't have to be LinkButton_Command, anything else that dose the job without doing postback is fine. – Nazo Tajrian Mar 12 '12 at 22:42
  • @NazoTajrian: This will be an asynchronous postback and only the content of `UpdatePanel1` will be updated by the `Update` method. If this is not the case(check by debugging [`ScriptManager.GetCurrent(Page).IsInAsyncPostback`](http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.isinasyncpostback.aspx)), you need to wrap your UserControl in another UpdatePanel. – Tim Schmelter Mar 12 '12 at 22:47
  • Tim, thank you very much it is working great, there is one little issue, just for test I added a button with ID "xyz" and label in this ascx control that I am loading it by clicking the navigation LinkButton, when I click that "xyz" button it should change the label text but instead everything in placeholder disappears, is it clear? – Nazo Tajrian Mar 13 '12 at 02:12
  • @NazoTajrian: It is difficult to tell what's going wrong but i'm sure that it has nothing to do with this question or UpdatePanels in general. Instead i assume that you're not (re)loading the dynamical controls on every postback in page_load at the latest. Ask another question if you don't get it going. – Tim Schmelter Mar 13 '12 at 08:01
  • I see, thanks, I did post the issue in different question: http://stackoverflow.com/questions/9689067/custom-usercontrol-content-in-updatepanel-disappears-on-button-click-postback – Nazo Tajrian Mar 13 '12 at 17:34
  • It helped, thank you. Please, just update the UpdatePanel property "UpdateModeMode" to "UpdateMode". – drazen Oct 24 '13 at 15:53