Jul 14th, 2004, 02:31 AM
#1
Thread Starter
Frenzied Member
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
Jul 14th, 2004, 03:18 AM
#2
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:
btnOpen.Attributes.Add("onclick", "openNewWindow();")
Jul 14th, 2004, 03:47 AM
#3
Thread Starter
Frenzied Member
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.
Jul 14th, 2004, 04:00 AM
#4
VB Code:
Page.RegisterHiddenField("__EVENTTARGET", "btnDefault")
where btnDefault is the name of your button.
Jul 14th, 2004, 04:27 AM
#5
Thread Starter
Frenzied Member
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
Jul 14th, 2004, 05:18 AM
#6
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 .
Jul 14th, 2004, 05:52 AM
#7
Thread Starter
Frenzied Member
Nothing happens when I try to view the source
Jul 14th, 2004, 05:53 AM
#8
Thread Starter
Frenzied Member
I have no JS connected to the button "Login"
Jul 15th, 2004, 01:31 AM
#9
Thread Starter
Frenzied Member
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
Jul 15th, 2004, 04:50 AM
#10
so it's exactly the same as what i have:
Attached Files
Jul 15th, 2004, 04:54 AM
#11
ok so i won't post the vb file, why is .cs a valid extension for uploading but not .vb?
VB Code:
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents RadioButtonList1 As System.Web.UI.WebControls.RadioButtonList
Protected WithEvents btnClose As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Button3 As System.Web.UI.WebControls.Button
Protected WithEvents btnDefault As System.Web.UI.WebControls.Button
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents btnDefault2 As System.Web.UI.WebControls.Button
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Page.RegisterStartupScript("MyScript", "<script language=javascript>" & _
' "function AlertHello() { alert('Hello ASP.NET'); }</script>")
Page.RegisterClientScriptBlock("MyScript", "<script language=javascript>" & _
"function AlertHello() { alert('Hello ASP.NET'); }</script>")
Button2.Attributes.Add("onclick", "AlertHello()")
Page.RegisterHiddenField("__EVENTTARGET", "btnDefault2")
'btnClose.Attributes.Add("onclick", "window.close();")
btnClose.Attributes.Add("onclick", "closewin();")
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Dim a As Int16
a = 16
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim x, y As Int16
x = 2
y = 0
Dim z As Int16 = x / y
End Sub
Sub btnDefault_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim i As Int16 = 2
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim a As Char = "w"
End Sub
Private Sub btnDefault2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDefault2.Click
Dim i As Int16 = 2
End Sub
End Class
Jul 15th, 2004, 06:34 AM
#12
Thread Starter
Frenzied Member
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
Jul 15th, 2004, 07:22 AM
#13
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
Forum Rules
Click Here to Expand Forum to Full Width