Solved

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

Posted on 2011-09-29
3
292 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 …
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…

861 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now