SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with update panel in ajax.net

    Hi, i am having problem with ajax.net updatepanel. I have an aspx page , in which i have an update panel . In the update panel i have place holder and i load a user control dynamically into the placeholder via ajax call. I have a button out side of the update panel some where else in the aspx page , which i use as a trigger to the update panel.Problem is that every things works fine for the first time, but on second click on the button , it posts back instead of using an asynchronous call to server.

    Here is my code behind file :

    public partial class dlgTesting : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    Button1.Attributes.Add("onclick", "document.getElementById('"+myDialogue.ClientID+"').style.display='block';");
    AsyncPostBackTrigger foo = new AsyncPostBackTrigger();
    foo.ControlID = "Button1";
    foo.EventName = "Click";
    UpdatePanel1.Triggers.Add(foo);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    plcHdrDlg.Controls.Clear();
    Control temp = LoadControl("~/UserControls/dialogues/tempDialogue.ascx");
    plcHdrDlg.Controls.Add(temp);
    UpdatePanel1.Update();
    }
    }



    I would appreciate if some can help me identify and remove the problem.

    Kind Regards,

    Ahsan

  2. #2
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dynamically creating/inserting controls is an advanced topic. The most common problem is that unlike other control state, asp.net has no way to re-create the controls on postback. I.e. *you* must ensure that the control is recreated on each an every postback. This would be a problem with or without the updatepanel.

    The best way o recreate the control is to record something in the viewstate of the page or the containing control which tells your code to recreate the control on page_load on postbacks.

  3. #3
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually , its more of an issue of update panel and adding triggers programmatically then the issue of adding user controls dynamically. (Please correct me if i am wrong)... so .. i was actually looking if some one has encountered any similar problem ?

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2007
    Posts
    1,274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try the code without the UpdatePanel; that would settle it. Do you have any code which will recreate the dynamically inserted control upon postbacks/callbacks? I can't see it in your Page_Load. Trust me, this *is* a problem, update panel or not.

    Looking at your code, I think you are going about the update panel wrong. You should set the triggers part of the update panel to reference the button. By default the update panel will intercept any postbacks from inside itself and convert them to callbacks with partial page renderings. It can also intercept events (i.e. clicks) of buttons/controls from the outside, but you must specify so explicitly.

    Instead of trying to set this up programatically you should set it up declaratively in the template:

    Code ASP:
            <asp:Button ID="Button1" runat="server" Text="Button" />
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <%= DateTime.Now %>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •