views:

443

answers:

1

I have an AJAX modalpopup which I would like to launch when a page loads. Currently, I have the following in my Page_Load:

HtmlGenericControl myBody = (HtmlGenericControl)Master.FindControl("thebody");
myBody.Attributes.Add("onload", "openMP();")

This successfully injects the onload function (I can tell by looking at the source). However, the onload function never seems to fire. Any advice would be helpful. Thanks.

* Changes following question *

Relevant code in master page:

<asp:LoginView ID="LoginView1" runat="server">
          <LoggedInTemplate>
          <a href="logout.aspx">

Relevant code in child page:

    if (!Page.IsPostBack)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", "openMP();", true);
        Response.Write("Test");
    }
+1  A: 

You may want to look at ClientScript.RegisterStartupScript. Here's an example of how you could implement it:

Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript",
   "openMP();", true);

Within the Page_Load event, this would fire every time the page is loaded.

EDIT: For clarity sake, here's an example of what I'm talking about in my comment:

ASPX Page Code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript",
            "openMP();", true);
}

ASPX Page:

<Form ID="Form1">
.
.
.
</Form>
<Script Language="javascript">
    function openMP() {
        ...
    }
</Script>
CAbbott
I put this in my Page_Load event, and the page seems to refresh itself continually (I never see the modal popup, just the primary page reloading over and over again). Any idea why this might be? Thanks for your help.
I'm guessing that your openMP function is performing a postback? If so, that would cause you to get stuck in an infinite loop. You may want to conditionalize the RegisterStartupScript to avoid that.
CAbbott
The following is in openMP: document.getElementById("ctl00_LoginView1_Button1").click();(Button 1 is the target button which launches the modalpopup.) How can I avoid the infinite loop? Thanks.
Ok, your function is performing a button click which is causing the postback. Within the Page_Load event you can add: if (!Page.IsPostback) Page.ClientScript.RegisterStartupScript(....)
CAbbott