Jump to content

Updatepanels automatically calls the "Page_Load" function?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
ManyTimes

ManyTimes

    Newbie

  • Members
  • PipPip
  • 14 posts
Hello

Got a big issue here with buttons inside an updatepanel. The button fires its normal OnClick function (which is added through the attribute "OnClick"), but when this function is fired, the "Page_Load" function is also automatically fired, when a panel in an .aspx file is updated... Hence; the update do happen, but since it is only the updatepanel is rendered again, the update do not show for the user, but I know it happened, I do not want that. :)

1. A masterpage with the content:
    
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
2. A content page to the newly created masterpage:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <asp:ScriptManager runat="server" ID="SCM" EnablePartialRendering="true"></asp:ScriptManager>
    <asp:Button ID="btnUpdate" Text="Update" runat="server" onclick="btnUpdate_Click" />
    <asp:Label runat="server" ID="lblMaster" BackColor="LightGreen"></asp:Label>

    <asp:UpdatePanel runat="server" ID="up1" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="btn1" Text="Btn1" runat="server" onclick="btn1_Click" />
        <asp:Label runat="server" ID="lbl11"></asp:Label>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btn1" EventName="Click" />
    </Triggers>
    </asp:UpdatePanel>
    
    <br /><br />
    
    <asp:UpdatePanel runat="server" ID="up2" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="btn2" Text="Btn2" runat="server" onclick="btn2_Click" />
        <asp:Label runat="server" ID="lbl22"></asp:Label>
    </ContentTemplate>
        <Triggers><asp:AsyncPostBackTrigger ControlID="btn2" EventName="Click" /></Triggers>
    </asp:UpdatePanel>

</asp:Content>
3. Code behind (C#) to the newly created content page
       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblMaster.Text = "Master.aspx Loaded: " + DateTime.Now.ToLongTimeString();
                lbl11.Text = "UpdatePanel 1 : ";
                lbl22.Text = "UpdatePanel 2 : ";
            }
            else
            {
                lblMaster.Text += " Updated? " + DateTime.Now.Second;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

        }

        protected void btn1_Click(object sender, EventArgs e)
        {
            lbl11.Text += DateTime.Now.Second + " ";
        }

        protected void btn2_Click(object sender, EventArgs e)
        {
            lbl22.Text += DateTime.Now.Second + " ";
        }
The problem in detail:
Click "button1" and the first updatepanel updates.
Click "button2" and the second updatepanel updates.

Now click the third button "Update" and see that the label outside the two updatepanels, has also been "updated" (but not displayed), when we clicked button1 and button2.

How can I fix this "issue"? Is it possible? The "Update?" text should only be written to the label as many times as we have clicked the button "btnUpdate"...

ed: What I've tried:
Adding in the page_load " SCM.RegisterAsyncPostBackControl(btn1);", did not work!

Of course I can change the "event" from "Page_Load" to "Page_Init", but I need the Page_Load function there.
-> of course I can create a session variable to make sure the function calls in Page_load() is not triggered when a button in a updatepanel is clicked
->-> but still, I want to refuse/remove the Page_Load(); call when a button inside a .aspx file is clicked! :)

Edited by ManyTimes, 12 May 2010 - 12:38 PM.


#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts

ManyTimes said:

Now click the third button "Update" and see that the label outside the two updatepanels, has also been "updated" (but not displayed), when we clicked button1 and button2.
What do you mean by that ? , You clicked 3rd button & see the page is posted back because it is outside the update panels ,also postback invoked the btnUpdate3 OnClick event. [ Now why did btn1 & btn2 come again ]


ManyTimes said:

How can I fix this "issue"? Is it possible? The "Update?" text should only be written to the label as many times as we have clicked the button "btnUpdate"...

Unless you do this It wont

protected void btnUpdate_Click(object sender, EventArgs e)
        {
                lblMaster.Text = "Master.aspx Loaded: " + DateTime.Now.ToLongTimeString();
                lbl11.Text = "UpdatePanel 1 : ";
                lbl22.Text = "UpdatePanel 2 : ";
        }

I need to tell you , always avoid extraneous information that the answerer doesn't need.
just post what needs to be done , But what happened

#3
ManyTimes

ManyTimes

    Newbie

  • Members
  • PipPip
  • 14 posts
>>What do you mean by that ?
It simply means that the "Page_load" function has been called on, when button1 and 2 has been clicked... Which I do not want!

You cannot see it (in my "code" above), unless you do a full postback, then the label "lblMaster" has been updated, for all the times we've clicked btn1 and/or btn2...
BUT; the functions called for the OnClick attribute on both of the buttons (1 and 2) do not add more text to the lblMaster, hence the page_load has been called when we've clicked them...

Want to configure when the page_load function should be called! In the sample above, it should only be called upon first load and a postback, not when clicking btn1 or btn2...

>>Unless you do this It wont
You really fail to see my issue...

>>just post what needs to be done , But what happened
Lol

Edit: But doubt it is possible, I believe Microsoft "hard-coded" the page_load calls... so :(

#4
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
your code seems to run well & good as you expected . I am not able to replicate what you said ? could you try re-installing the Ajax framework & try again

#5
l@mbd@

l@mbd@

    Newbie

  • Members
  • PipPip
  • 27 posts
Funny, 99% of my aspx pages don't have a page_load method. noobs crack me up.

#6
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts

l@mbd@ said:

Funny, 99% of my aspx pages don't have a page_load method. noobs crack me up.

well that did answer the question did it lol :D. Put your comments onto General section.

#7
l@mbd@

l@mbd@

    Newbie

  • Members
  • PipPip
  • 27 posts
What question is there? I can't figure out what he's asking for. I look through the code, run the code, all works as I would expect.

The OP expressed a wish to "remove/refuse" the Page_Load method, and based on my reply, I hope it's clear that doing so is perfectly acceptable.

Cheers




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users