ScriptManager.RegisterStartupScript not working if Response.redirect used

In my page load based on certain condition i call UserMsgBox(“user Not Authorize”) and it is working. while if i call Response.Redirect(“Home.aspx”) after that it is not showing alert message.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.QueryString("val") IsNot Nothing Then
         ...
         Else
               UserMsgBox("user Not Authorize")
               Response.Redirect("Home.aspx")
            End If
End Sub

 Private Sub UserMsgBox(sMsg As String)
        Dim jScript As String = "alert('" + sMsg + "');"
        ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "onclick", jScript, True)
 End Sub

i want alert message to be popup and Response.Redirect(“Home.aspx”) also work. any would be appreciated .

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x