How do you make a webform button run an onClick Javascript

  • Thread starter John Timney \(Microsoft MVP\)
  • Start date
J

John Timney \(Microsoft MVP\)

Theres an example below, just transfer the server side click event to code
behnd and it should work

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP

Cathy said:
Has anyone ever made a webform button run an onClick event that is a
Javascript in the HTML code and then after running the script make it go
into the code-behind? An HTML button will run the onClick event but will not
return to the code-behind. I need to get back to the code, which the webform
button will do but I can't make it execute the script.

Any help will be appreciated. Thanks.

<html>
<head>
<script language="VB" runat="server">

Sub Page_Load( sender as Object,e as EventArgs)


'Form the script that is to be registered at client side.
Dim scriptString as String = "<script language=JavaScript> function
onClientClick() {"

ScriptString += "var retVal = window.confirm('Click OK to continue.
Click Cancel to stop.');"
ScriptString += "window.alert(retVal);"
ScriptString += "if (retVal){"
ScriptString += "window.alert('MVP Code is doing the Postback!');"
ScriptString += "return true;"
ScriptString += "}"
ScriptString += "else"
ScriptString += "{"
ScriptString += "window.alert('MVP Code is NOT Doing the
Postback!');"
ScriptString += "return false;"
ScriptString += "}"
ScriptString += "}<"
scriptString += "/"
scriptString += "script>"

If(Not IsClientScriptBlockRegistered("clientScript"))
RegisterClientScriptBlock("clientScript", scriptString)
End If


End Sub


Sub button_onClick( sender as Object,e as EventArgs)
Response.Write("Postback as requested!!!" + DateTime.Now())
End Sub


</script>
</head>
<body topmargin="20" leftmargin="10">
<form id="myForm" runat="server">


<input type="button" value="OK" onserverclick="button_onClick"
onclick="if(!onClientClick()){return false;}"
runat="server">

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

Cathy

Has anyone ever made a webform button run an onClick event that is a
Javascript in the HTML code and then after running the script make it go
into the code-behind? An HTML button will run the onClick event but will not
return to the code-behind. I need to get back to the code, which the webform
button will do but I can't make it execute the script.

Any help will be appreciated. Thanks.
 
C

Cor Ligthert

Cathy,

Use VBNet webform project, and than
drag a webform button and a textbox on your page and paste in this in the
code behind

\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Me.TextBox1.Text = "Click the button"
Me.Button1.Text = "Send Mail"
Me.Button1.Attributes("onClick") = _
"window.location='mailto:[email protected]?subject=Cor demo&body=I
hope this helps?';"
End If
End Sub
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Me.TextBox1.Text = "I was on the ServerSide"
Me.Button1.visible = False
End Sub
///

I hope this helps?

Cor
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top