Solved

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

Posted on 2011-09-29
3
297 Views
Last Modified: 2012-08-13
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
Comment
Question by:lino_evolution
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Expert Comment

by:drain
ID: 36816226
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
 

Accepted Solution

by:
lino_evolution earned 0 total points
ID: 36816437
I managed to solve this problem. I used RegisterScriptBlock insteand and worked like a charm.
0
 

Author Closing Comment

by:lino_evolution
ID: 36908439
Used Another command
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question