Scriptmanager.Registerstartupscript Code Not Working - Why

ScriptManager.RegisterStartupScript code not working - why?

Off the top of my head:

  • Use GetType() instead of typeof(Page) in order to bind the script to your actual page class instead of the base class,
  • Pass a key constant instead of Page.UniqueID, which is not that meaningful since it's supposed to be used by named controls,
  • End your Javascript statement with a semicolon,
  • Register the script during the PreRender phase:

protected void Page_PreRender(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey",
"alert('This pops up');", true);
}

ScriptManager.RegisterStartupScript is not working inside a method

Use ClientScript class instead of ScriptManager

private string test()
{
if(somecondition)
{
ClientScript.RegisterStartupScript(Page.GetType(), "HideSlider1", "HideSlider();", true);
}
return stringvalue;
}

ScriptManager.RegisterStartupScript() is not working after putting .aspx file inside a different folder

There are a few issues that could be causing your existing problem :

Ensure Proper Referencing

All the RegisterStartupScript() method does is actually register a specific script to be added to the page (and often executed) when everything has been loaded. Your existing code will call the ImgDivPopup() when this occurs, which would generally work assuming that a <script> reference existing with that function defined :

<!-- Ensure you are referencing the appropriate file so you can access the function -->
<script src='<%= ResolveUrl("~/Scripts/your-imgdivpopup-file.js") %>'></script>

This is just an example of how you might reference your file with your function defined, but if you are using an external file, you'll need this.

Possible Race Condition

If you are referencing the file and it has any other dependencies, such as another client-side library, then you could consider adding an explicit delay in calling your function to give things time to catch up :

// Call your function with a 10ms delay
ScriptManager.RegisterStartupScript(Page, GetType(), "disp_confirm", "setTimeout(function(){ ImgDivPopup();},10);", true);

Use Your Developer Tools (F12)

Since this is likely a client-side issue, consider using the Developer Tools (F12) within your browser. Check the Network and Console tabs to see if any errors are present (such as "function ImgDivPopup() is not defined", "{your-script-file} could not be found", etc.)

ScriptManager.RegisterStartupScript not working if Response.redirect used

You have to set some kind of session() value and get/grab/see this on the home.aspx page.

The problem is at "this instant" you are on the current page. When you run that userMsgbox code, it is injected into the CURRENT page. Unless that page makes a full round trip

eg:

User hits some button on web page.
Web page travels up to web server.
Code behind runs - maybe you modify some controls.
And THEN you as the last thing, inject the userMsgbox.

Now the page travels back down to client side.

The browser side then renders the page.
After it renders, then your injected js runs.

But, you NEVER let that page make it back to the client side, you have this:

Response.Redirect("Home.aspx")

So we never let the page travel back - we chop it off and say load another page.
But our current code context is the currently loaded page. Not the one you jumping to.

So, your current page-behind code is working on the current page - and controls you change/set/modify or script you inject is going to operate on that current loaded page.

With the re-direct then that page never makes the trip down to the client side - you sending to a NEW page home.aspx

Two ideas:

Use/set some kind of session().

Say like

Session("HomeMessage") = "user Not Authorize"
Response.Redirect("Home.aspx")

Then in the home.aspx page (load event), you need:

if isPostBack = false then
if Session("HomeMessage") <> "" then
Call UserMsgbox(Session("HomeMessage")
Session("HomeMessage") = ""
end if
end if

So now when the full home page renders browser side, then your msgbox you injected will also run. That might not be the best UI.

But with that setup (the re-direct occurring server side), then you can't do this on the previous page (well, actually our current page). But that page never travels back to the client side - you re-direct to home page, and that what gets send down to the browser.
(so you need that userMsgbox sub in the home.aspx page). I mean, if you set some text box on that page - we never see that either - since the code jumps to load a fresh new (different page) that the code behind is running against.

And this does mean that the home page will display FIRST and then the msgbox will show.

The other way?

Well, you could allow the current page to post back, and have the js script display the message AND ALSO do the jump to the other page.

This would thus allow the current page to display with the msgbox. You then hit ok, and the client side does the jump to home. (you remove your response.redirect.

So you could replace this:

UserMsgBox("user Not Authorize")
Response.Redirect("Home.aspx")

With say a routine like this:

UserMsgBoxJump("user Not Authorize","Home.aspx")

and

Private Sub UserMsgBox2(sMsg As String, strURL As String)

Dim jScript As String = "alert('" + sMsg + "');window.location.href = '" & strURL & "';"
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "onclick", jScript, True)

End Sub

So now, we let the page travel back (it will render), and after full page render, then your injected js can run. Then the msgbox will display.

And when the user hits ok, then the client side js code does the navigation to the target page.

So, it really depends on how you want to do this. Probably the 2nd idea is more flexible, since then you don't have to mess up the home page on-load event to check the session.

So keep the post/page life cycle in mind.

ScriptManager.RegisterStartupScript not working on ASP Button click event

ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('Fill all fields.');", true);

return;

RegisterStartupScript from code behind not working when Update Panel is used

You need to use ScriptManager.RegisterStartupScript for Ajax.

protected void ButtonPP_Click(object sender, EventArgs e)
{
if (radioBtnACO.SelectedIndex < 0)
{
string csname1 = "PopupScript";

var cstext1 = new StringBuilder();
cstext1.Append("alert('Please Select Criteria!')");

ScriptManager.RegisterStartupScript(this, GetType(), csname1,
cstext1.ToString(), true);
}
}


Related Topics



Leave a reply



Submit