How to call JavaScript function and pass parameter from VB or C#

Last post 04-24-2008 9:58 PM by jholland99. 10 replies.

Sort Posts:

  • How to call JavaScript function and pass parameter from VB or C#

    11-03-2007, 6:13 PM
    • Member
      41 point Member
    • jimmywang918
    • Member since 10-03-2004, 4:45 AM
    • Posts 27

    I have a javascript function in a .js file, I want to call a javascript function and pass some parameters to this function from VB or C#, I found some articles about this but most articles describe an event trigger in javascript from a button. I don't need trigger a event, I just want to pass paramater to javascript in client side to process something. Can I do this in ASP.NET 2.0 base.

    Thanks

    Jimmy 

     

  • Re: How to call JavaScript function and pass parameter from VB or C#

    11-03-2007, 8:55 PM
    • Contributor
      4,421 point Contributor
    • BrianOConnell
    • Member since 08-04-2002, 4:24 PM
    • Dublin, Ireland
    • Posts 828

    Put a hidden input in your html page - <input type="hidden" id="param1" value="" />

    Then in your c# or vb.net code set it to what you want to pass to your .js function - param1.value = "myparamvalue"

    Finally your javascript function can access this document.getElementById("param1").value

    Brian O'Connell (MCAD) - http://www.systemdotweb.com
  • Re: How to call JavaScript function and pass parameter from VB or C#

    11-06-2007, 5:55 AM
    • Member
      41 point Member
    • jimmywang918
    • Member since 10-03-2004, 4:45 AM
    • Posts 27

    Hi Brian O'Connell

    Thganks for your reply. Could I code like the following?

    <Script Language="VB" Runat="server">

    Sub MyVBFunction()

         param1.value="myparamvalue"

         MyJavaFunction()

    End Sub

    </Script>

     MyJavaFile.js

    MyJavaFunction(){

        var FromVBparam = document.getElementById("param1");

    }

    What I want is My VB function can call Java function straightly without any control event triggered. I think there should be some special codes in my VB codes to call Java function, but not like my codes as above,  it will not work. Thanks again.

    Sincerely 

    Jimmy

     

  • Re: How to call JavaScript function and pass parameter from VB or C#

    11-06-2007, 6:29 AM
    • Contributor
      4,759 point Contributor
    • naveenj
    • Member since 07-02-2007, 9:09 AM
    • India
    • Posts 864

    Hi jimmywang918,

    Try this.

    Sub MyVBFunction()

    param1.value = "myparamvalue"

    ClientScript.RegisterStartupScript(typeof(Page),"clientscript","MyJavaFunction()")

    End Sub

    dont forget to refer the js on the ASPX Page

     

    <script type="text/javascript" src="MyJavaFile.js">

    </script>

    Regards,

    Naveen

    Regards,
    Naveen

    Please remember to click Mark as Answer on the post that helps you
    View Blog
  • Re: How to call JavaScript function and pass parameter from VB or C#

    11-06-2007, 7:38 PM
    • Member
      41 point Member
    • jimmywang918
    • Member since 10-03-2004, 4:45 AM
    • Posts 27

    Hi Naveen

    Thank you very much. The following is my complete codes. No error alert, Nothing happened at all after I click the button. I don't know what's the problem with my codes. It seems the Java script only can be live by a control event initialization that like many people have done. It has broken my initial idea - calling javascript from VB or C# without a control event trigger.

     <%@ Page Language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
       Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) 

            Dim param1 As HtmlInputHidden = New HtmlInputHidden
            param1.value = "Hi! I am a VB Parameter"

            ' Define the name and type of the client scripts on the page.
            Dim csname1 As String = "ClientScript"
            Dim csname2 As String = "MyJava()"
            Dim cstype As Type = Me.GetType()

            ' Get a ClientScriptManager reference from the Page class.
            Dim cs As ClientScriptManager = Page.ClientScript

            ' Check to see if the client script is already registered.
            If (Not cs.IsClientScriptBlockRegistered(cstype, csname1)) Then

                cs.RegisterClientScriptBlock(cstype, csname1, csname2)

            End If

            cs.RegisterStartupScript(cstype, csname1, csname2) 

        End Sub
    </script><html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Untitled Page</title>
       
        <script type="text/javascript">
          function MyJava(){
             var FromVB = document.getElementById("param1").value;
             alert(FromVB);
          }
        </script>
    </head>

    <body>
        <form id="form1" runat="server">
            <input type = "hidden" id ="param1" />
            <div>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </div>

        </form>
    </body>
    </html>

  • Re: How to call JavaScript function and pass parameter from VB or C#

    11-07-2007, 2:14 AM
    Answer
    • Contributor
      4,759 point Contributor
    • naveenj
    • Member since 07-02-2007, 9:09 AM
    • India
    • Posts 864

    jimmywang918:
    I have a javascript function in a .js file, I want to call a javascript function and pass some parameters to this function from VB or C#,

    Hi jimmywang918,

    I will show you how to Pass the value on a TextBox to Hidden Variable on Button Click using JavaScript

    At ASPX Page

    ~~~~~~~~~~~~~~~~~~~~~~

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="VBLatestJSAlert.aspx.vb" Inherits="VBLatestJSAlert" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitled Page</title>

    <script type="text/javascript">

    function MyJava(elm)

    {

    var myHdnVar=document.getElementById("param1");

    alert(myHdnVar.value);

    myHdnVar.value = elm;

    alert(myHdnVar.value);

    }

    </script>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <input type="hidden" id ="param1" />

    <div>

    <asp:TextBox ID="txtTest" runat="server">

    </asp:TextBox>

    <asp:Button ID="Button1" runat="server" Text="Button" />

    </div>

    </div>

    </form>

    </body>

    </html>

    AT ASPX.CS Page

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

    Partial Class VBLatestJSAlert

    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim strTest As String = txtTest.Text

    ' Define the name and type of the client scripts on the page.

    Dim csname1 As String = "ClientScript"

    Dim csname2 As String = "MyJava('" + strTest + "')"

    Dim cstype As Type = Me.GetType()

    Page.ClientScript.RegisterStartupScript(Me.GetType(), csname1, csname2, True)

    End Sub

    End Class

     

    Try this out.

    Regards,

    Naveen

    Regards,
    Naveen

    Please remember to click Mark as Answer on the post that helps you
    View Blog
  • Re: How to call JavaScript function and pass parameter from VB or C#

    11-07-2007, 8:02 AM
    Answer
    • All-Star
      76,207 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,195
    • TrustedFriends-MVPs

    Try adding the runat="server" property to your Hidden element. Try this:

    <script runat="server">
       ' Protected WithEvents param1 As System.Web.UI.HtmlControls.HtmlInputHidden

       Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            param1.Value = "Hi! I am a VB Parameter"

            ' Define the name and type of the client scripts on the page.
            Dim csname1 As String = "ClientScript"
            Dim csname2 As String = "<script type=text/javascript>MyJava();</script>"
            Dim cstype As Type = Me.GetType()

            ' Get a ClientScriptManager reference from the Page class.
            Dim cs As ClientScriptManager = Page.ClientScript

            ' Check to see if the client script is already registered.
            If (Not cs.IsStartupScriptRegistered(cstype, csname1)) Then
                cs.RegisterStartupScript(cstype, csname1, csname2)
            End If
        End Sub
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Untitled Page</title>
       
        <script type="text/javascript">
     function MyJava()
     {
      var FromVB = document.getElementById('<%= param1.ClientID %>').value;
      alert(FromVB);
     }
        </script>
    </head>

    <body>
        <form id="form1" runat="server">
            <input type = "hidden" id ="param1" runat="server" />
            <div>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </div>

        </form>
    </body>
    </html>

    NC...

  • Re: How to call JavaScript function and pass parameter from VB or C#

    11-07-2007, 8:05 AM
    Answer
    • All-Star
      76,207 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,195
    • TrustedFriends-MVPs

    You might also try this with no hidden elements added:

    <script runat="server">
       Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            argValue = "Hi! I am a VB Parameter"

            ' Define the name and type of the client scripts on the page.
            Dim csname1 As String = "ClientScript"
            Dim csname2 As String = "<script type=text/javascript>MyJava('" + argValue + "');</script>"
            Dim cstype As Type = Me.GetType()

            ' Get a ClientScriptManager reference from the Page class.
            Dim cs As ClientScriptManager = Page.ClientScript

            ' Check to see if the client script is already registered.
            If (Not cs.IsStartupScriptRegistered(cstype, csname1)) Then
                cs.RegisterStartupScript(cstype, csname1, csname2)
            End If
        End Sub
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Untitled Page</title>
       
        <script type="text/javascript">
     function MyJava(argValue)
     {
      alert(argValue);
     }
        </script>
    </head>

    <body>
        <form id="form1" runat="server">
            <input type = "hidden" id ="param1" runat="server" />
            <div>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </div>

        </form>
    </body>
    </html>

    NC...

     

  • Re: How to call JavaScript function and pass parameter from VB or C#

    11-07-2007, 9:31 PM
    • Member
      41 point Member
    • jimmywang918
    • Member since 10-03-2004, 4:45 AM
    • Posts 27

    Hi Naveen, Brian O'Connell, NC01

    Many thanks to you all. Naveen's codes are working well. I have learned a lot from you guys. Thanks again! The hidden input is not working if assign a value to the hidden input before click the button, but after postback the hidden input is working with assigning a value. So I think my previous codes assign a value to the hidden input, after button click and postback the page status lost, so no value can be retrieved from the hidden input. That's what I understand. I don't know if my understanding is right.

     Yours Sincerely

     Jimmy

  • Re: How to call JavaScript function and pass parameter from VB or C#

    11-08-2007, 8:53 AM
    • All-Star
      76,207 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 14,195
    • TrustedFriends-MVPs

    If you are not doing a PostBack, how are you getting to the server-side code? If you are using AJAX, no this will not work.

    NC...

  • Re: How to call JavaScript function and pass parameter from VB or C#

    04-24-2008, 9:58 PM
    • Member
      14 point Member
    • jholland99
    • Member since 03-27-2008, 1:27 AM
    • Posts 24

    Thank you for posting this code! This is the first VB.NET to JS example that works for what I needed!

    Jason 

Page 1 of 1 (11 items)