Results 1 to 13 of 13

Thread: Running a JS from an Asp:button?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question Running a JS from an Asp:button?

    Is that possible? I have tried this:
    Code:
    <asp:button id=btnOpen OnClick="Javascript: openNewWindow();" tabIndex=20 Runat="server" CssClass="knap2" Text="Åbn fil"></asp:button>
    where

    Code:
    <script language=javascript>
    		function openNewWindow()
            {
    			var winName = "";
                var winFeatures = "width=520,height=640,top=0,left=0,status";
                var winURL = "http://localhost/Prisberegning2004/udskrift.aspx"
                var w;
                w = window.open(winURL,winName,winFeatures);
            }
    </script>
    But it doesnt't work

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    There's a number of ways you could do it, 1 way would be to leave your javascript where it is on your .aspx page and add this line to page_load
    VB Code:
    1. btnOpen.Attributes.Add("onclick", "openNewWindow();")

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    GREAT GREAT, just what I needed!


    Maybe you could have an idea of how I can execute a click-event when I hit enter in a textbox.

    I have a login consisting of txtUser, txtPassword and btnLogin. When I have entered username and password and hit enter I wish to execute the btnLogin_click.

  4. #4
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    VB Code:
    1. Page.RegisterHiddenField("__EVENTTARGET", "btnDefault")
    where btnDefault is the name of your button.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Hmmm, I've tried that under Page_load, but nothing happens.
    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Page.RegisterHiddenField("__EVENTTARGET", "Login")
        End Sub

  6. #6
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    hmm works for me everytime just tried a page with multiple buttons and it works for each one. when you run the page and view source do you get this line
    Code:
    <input type="hidden" name="__EVENTTARGET" value="Login" />
    EDIT: There's a problem if you add a JS attribute to the button aswell as the above code, could that be the problem?
    Last edited by Fishcake; Jul 14th, 2004 at 05:35 AM.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Nothing happens when I try to view the source

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    I have no JS connected to the button "Login"

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Originally posted by Fishcake
    hmm works for me everytime just tried a page with multiple buttons and it works for each one. when you run the page and view source do you get this line
    Code:
    <input type="hidden" name="__EVENTTARGET" value="Login" />
    EDIT: There's a problem if you add a JS attribute to the button aswell as the above code, could that be the problem?
    Please send me the code you're using, then I will try to find out what goes wrong

  10. #10
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    so it's exactly the same as what i have:
    Attached Files Attached Files

  11. #11
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    ok so i won't post the vb file, why is .cs a valid extension for uploading but not .vb?

    VB Code:
    1. Public Class WebForm1
    2.     Inherits System.Web.UI.Page
    3.  
    4. #Region " Web Form Designer Generated Code "
    5.  
    6.     'This call is required by the Web Form Designer.
    7.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    8.  
    9.     End Sub
    10.     Protected WithEvents RadioButtonList1 As System.Web.UI.WebControls.RadioButtonList
    11.     Protected WithEvents btnClose As System.Web.UI.WebControls.Button
    12.     Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    13.     Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    14.     Protected WithEvents Button3 As System.Web.UI.WebControls.Button
    15.     Protected WithEvents btnDefault As System.Web.UI.WebControls.Button
    16.     Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    17.     Protected WithEvents btnDefault2 As System.Web.UI.WebControls.Button
    18.  
    19.     'NOTE: The following placeholder declaration is required by the Web Form Designer.
    20.     'Do not delete or move it.
    21.     Private designerPlaceholderDeclaration As System.Object
    22.  
    23.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    24.         'CODEGEN: This method call is required by the Web Form Designer
    25.         'Do not modify it using the code editor.
    26.         InitializeComponent()
    27.     End Sub
    28.  
    29. #End Region
    30.  
    31.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    32.         'Page.RegisterStartupScript("MyScript", "<script language=javascript>" & _
    33.         '       "function AlertHello() { alert('Hello ASP.NET'); }</script>")
    34.         Page.RegisterClientScriptBlock("MyScript", "<script language=javascript>" & _
    35.                "function AlertHello() { alert('Hello ASP.NET'); }</script>")
    36.         Button2.Attributes.Add("onclick", "AlertHello()")
    37.         Page.RegisterHiddenField("__EVENTTARGET", "btnDefault2")
    38.  
    39.         'btnClose.Attributes.Add("onclick", "window.close();")
    40.         btnClose.Attributes.Add("onclick", "closewin();")
    41.     End Sub
    42.     Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
    43.         Dim a As Int16
    44.         a = 16
    45.     End Sub
    46.  
    47.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    48.         Dim x, y As Int16
    49.         x = 2
    50.         y = 0
    51.         Dim z As Int16 = x / y
    52.     End Sub
    53.  
    54.     Sub btnDefault_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    55.         Dim i As Int16 = 2
    56.     End Sub
    57.  
    58.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    59.         Dim a As Char = "w"
    60.     End Sub
    61.  
    62.     Private Sub btnDefault2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDefault2.Click
    63.         Dim i As Int16 = 2
    64.     End Sub
    65. End Class

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Hi again, thanks for your code. The code works fine. But when I apply a new textbox (I have two textboxes in my Login app) something strange happens. When I hit enter the event for closing the window is running instead of the btnDefault2-event

  13. #13
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    Yep i've just done the same and got the same results.

    I want to sort this out so i'll going to try some things.......

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