Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Library to convert HTML to PDF. 8 45
Check if number is currency 15 29
onOpen 14 41
Getfiles in vb.net 28 18
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

758 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

20 Experts available now in Live!

Get 1:1 Help Now