Results 1 to 10 of 10

Thread: How to fire off Java script function from VB code in VB .Net

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    How to fire off Java script function from VB code in VB .Net

    Hi everyone. I have an input box function written in java. I have a couple of questions.

    First, how would I start that function from the middle of a precedure from my visual basic sub routine? I don't want it to always run when this button is clicked. What I'm doing is checking a database for some specific data, if it's not found, the popup box function needs to open up so they can enter a value in.

    also, can I pass some value, say a session value from the vb code to the java function?

    Lastly, how could I pass the value they entered in back to the vb process that is running.

    Following is the java I tried but I don't think it will use the Session variable I'm trying.
    Code:
    <script language="javascript">
         Function (TermID)
             var TermID = prompt("Enter Terminal ID for " & Session("st"));
    </script>
    I know there are buttons out there that I could click and have them enter a value, but like I said I need this to run sometimes, not everytime the submit button is clicked.

    Thanks!
    David Wilhelm

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Within your VB function you can do something like this:
    Code:
    ....if data is not found
    
    Response.Write("<script language='javascript'>YourJSFunctionName(" & YourParameterValue & ")</script>")

  3. #3
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    The most accepted way to do this is as follows:-

    if <your data code> then
    dim sb as new system.text.stringbuilder
    sb.append("<script language=""javascript"">" & vbcrlf)
    sb.append("Function (TermID)" & vbcrlf)
    sb.append("var TermID = prompt(""Enter Terminal ID for " & Session("st") & """);" & vbcrlf)
    sb.append("</script>")
    page.registerstartupscript("myscript", sb.tostring)
    sb = nothing
    end if

    What this does is correctly places the javascript where it is sure to run once the page has loaded (and all objects are loaded and rendered on the page). It also allows you to access the code and remove it by using the name you give it, myscript, in the case above. The vbcrlf is just to make the code readable in the pages source.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Thanks for the post you guys. However there is still a problem.

    I need this input box to pop up right when it encounters it in the code. This is not happening.

    The input box does not show up until after the procedure has completed which is too late.

    Also, is there anything like a Yes No button that I can have pop up at the begining of the code and have it process the answer without waiting until the procedure has already run?

    Thanks again.
    David Wilhelm

  5. #5
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Use JS and override the pre render event of the base class. Or use JS in the page load event.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    I don't know what you mean there. Could you possibly give a code example please?

    Thanks
    David Wilhelm

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    bump
    David Wilhelm

  8. #8
    Hyperactive Member greg_quinn's Avatar
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    366
    Hi Indy,

    I know what you are asking, what happens is usually you response.write the javascript, and it happens after everything else, you need it before everything else gets rendered on the page? Am I correct?

    To do this... do something like this...

    Dim jScriptString as String

    jScriptString = "<SCR" + "IPT>"
    jScriptString += "alert('Hello');"
    jScriptString += "</SCR" + "IPT>"

    lblScript.Text = jScriptString

    Now put lblScript (an asp:label) at the top of your page, the script will then execute before anything else is rendered...

  9. #9
    Lively Member SpagettiProg's Avatar
    Join Date
    Dec 2004
    Location
    Miami, Florida
    Posts
    82

    Re: How to fire off Java script function from VB code in VB .Net

    I'm having a similar problem and I don't see an answer in the post above.

    Here's my scenario.....

    I have placed the following javascipt in the html side of my aspx page
    (hard coded)

    <script language="javascript">
    function blahblah()
    {
    alert('Test')
    }
    </script>


    Now let's look at my code behind....

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    If (Session("callscript" = true) Then
    ' Here I need to be able to call the script that is already hard coded into my page
    End If

    End Sub


    I don't think it's possible but how would you call an existing script in asp.net code ?

  10. #10
    Hyperactive Member greg_quinn's Avatar
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    366

    Re: How to fire off Java script function from VB code in VB .Net

    What you'll need to do is write the script into a label control. Because you can't call a script that hasn't been rendered to the HTML page yet..

    Make sure the script is in the head. And your label tag in the body...

    Then do something like

    <script language = "JavaScript">
    function sayHello()
    {
    alert('Hello');
    }
    </script>

    <asp:label id = "lblScript" runat = "server" />


    Then in your server-side code...

    Sub CallMyScript()
    lblScript.Text = "sayHello();"
    End Sub
    He who has conquered himself, is far greater than he who has conquered a thousand men... - The Buddha

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width