How to call a client-side vbscript from server-side button

Discussion in 'ASP .Net' started by alan, Sep 15, 2005.

  1. alan

    alan Guest

    Hi all,

    I need to pass a variable to the client-side vbscript and when the button is
    pressed invoke the script (SomeSub()).
    The problem is, that the "blablabla" appears only after second pressing of
    the button.
    Probably there is something with the postback, but I cannot figure it out.

    My .aspx :

    <asp:Button id="Button3" runat="server" Text="Butt"></asp:Button>
    <script language="VBscript" type="text/vbscript">
    Sub SomeSub()
    Dim mystr
    mystr = "<%= variable %>"
    document.write("variable")
    End Sub
    </script>

    On my .vb file I'v got:

    Public variable As String = "blablabla"

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button3.Click
    Button3.Attributes.Add("onclick", "SomeSub()")
    End Sub

    Please help me.
    alan, Sep 15, 2005
    #1
    1. Advertising

  2. Hi Alan

    Yep, you're right. Your VBScript will always fire on the second click. This
    is because your Attributes.Add occurs when the button is clicked, so you MUST
    click the button in order for the VBs to be attached. You click it again,
    it's attached, and it works!

    To get that to work, get rid of the button handler (Private Sub
    Button3_Click()) - unless you need it for something else of course, and add
    the Attributes.Add to your page load...

    Private Sub Page_Load()
    Button3.Attributes.Add("onclick", "SomeSub()")
    End Sub

    Hope this helps,


    Dan



    "alan" wrote:

    > Hi all,
    >
    > I need to pass a variable to the client-side vbscript and when the button is
    > pressed invoke the script (SomeSub()).
    > The problem is, that the "blablabla" appears only after second pressing of
    > the button.
    > Probably there is something with the postback, but I cannot figure it out.
    >
    > My .aspx :
    >
    > <asp:Button id="Button3" runat="server" Text="Butt"></asp:Button>
    > <script language="VBscript" type="text/vbscript">
    > Sub SomeSub()
    > Dim mystr
    > mystr = "<%= variable %>"
    > document.write("variable")
    > End Sub
    > </script>
    >
    > On my .vb file I'v got:
    >
    > Public variable As String = "blablabla"
    >
    > Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles Button3.Click
    > Button3.Attributes.Add("onclick", "SomeSub()")
    > End Sub
    >
    > Please help me.
    >
    >
    >
    >
    =?Utf-8?B?ZGhucml2ZXJzaWRl?=, Sep 15, 2005
    #2
    1. Advertising

  3. alan

    alan Guest


    > Hope this helps,
    >
    >
    > Dan


    Yes, it works :)
    Thank You Dan
    alan, Sep 15, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Laskie
    Replies:
    1
    Views:
    1,559
    Carl Prothman [MVP]
    Aug 21, 2003
  2. Eitan
    Replies:
    2
    Views:
    512
    Eitan
    Jun 4, 2005
  3. COHENMARVIN
    Replies:
    2
    Views:
    865
    Jason Kester
    Oct 20, 2005
  4. David Shorthouse

    server-side vbscript call with javascript client-side

    David Shorthouse, Feb 2, 2005, in forum: ASP General
    Replies:
    2
    Views:
    346
    Mark Schupp
    Feb 3, 2005
  5. Replies:
    2
    Views:
    265
Loading...

Share This Page