• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

Calling the Right Javascript Function on Page Load in VB.NET

Hello,

I need to call a javascript method from VB on page load. I have one problem and I know there are two solutions.

The problem I have now is on page load, depending on the action I want to call a javascript function, I use the ClientScript.RegisterStartupScript command.  Now in order to get this command to work on page load I had to insert the javascript method call at the end of the page.  The problem with this is that I have three methods and when I put all three methods on the page the last one is executed, negating the effect of the first two.  How do I prevent this?  

OR

How do i use pass variables using the ClientScript.RegisterStartupScript.  I tried doing it and I get an 'undefined' variable on the receving end.  Thats why I have three functions basically doing the same thing but with a different Stored Procedure.
VB.NET ON Page Load

If Request.QueryString("action") <> Nothing Then
            Select Case Request.QueryString("action")
                Case "GetMostHelpfulBlogs"
                    pnlBlogs.Visible = True
                    ClientScript.RegisterStartupScript(Me.GetType, "ClientScript", "getHelpfulBlogs()", False)
                Case "GetMostCommentedBlogs"
                    pnlBlogs.Visible = True
                    ClientScript.RegisterStartupScript(Me.GetType, "ClientScript", "getCommentBlogs()", False)
                Case "GetMostRecentBlogs"
                    pnlBlogs.Visible = True
                    ClientScript.RegisterStartupScript(Me.GetType, "ClientScript", "getRecentBlogs()", False)
                Case "default"
                    pnlDefault.Visible = True
                Case Else
                    pnlDefault.Visible = True
            End Select
        Else
            pnlDefault.Visible = True
        End If


on ASPX Page


 <script type="text/javascript">
        getHelpfulBlogs()
</script>
 <script type="text/javascript">
        getCommentBlogs()
</script>
 <script type="text/javascript">
        getRecentBlogs()
</script>

Open in new window

0
lino_evolution
Asked:
lino_evolution
  • 2
1 Solution
 
drainCommented:
Something is not working right if you have to insert the function call's at the bottom of the page. If you view source does the onload actually have anything in it?

On #2 you should be able to do
ClientScript.RegisterStartupScript(Me.GetType, "ClientScript", "getHelpfulBlogs(""" & Request.QueryString("action")  & """)", False)

A third alternative, is that given that you could just call one function and have javascript parse the querystring, and call the correct function
something like this (presuming there isn't anything else in your querystring
var action = window.location.search.substring(1).replace("action=","")
0
 
lino_evolutionAuthor Commented:
I managed to solve this problem. I used RegisterScriptBlock insteand and worked like a charm.
0
 
lino_evolutionAuthor Commented:
Used Another command
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now