Thursday, March 29, 2012

Onserverclick event not running (inc. Code)

Hello All,

I am trying to run both client and server events when clicking a button.
Since the code i wrote i tried this enclosed code as a test but it won't work please tell me what i'm doing wrong:

(P.S. I posted the browser source as there is almost no code behind and this, i think, shows more details)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm4</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"
</HEAD>
<body MS_POSITIONING="GridLayout">
<form name="Form1" method="post" action="WebForm4.aspx" id="Form1">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" value="dDwxOTI0MjI4NTEyOzs+hLaPGDf9/EXBiKS+L6TBywF4Aw4=" /
<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
theform = document.forms["Form1"];
}
else {
theform = document.Form1;
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script
<input language="javascript" onclick="Javascript: return test(); __doPostBack('Button1','')" name="Button1" id="Button1" type="button" style="Z-INDEX: 101; LEFT: 248px; POSITION: absolute; TOP: 120px" value="Button" />
</form>
<script language="javascript">
function test() {
alert("hi!");
}
</script>
</body>
</HTML

TIA,

GK582Hi,
Can I know how it won't work? Client script not run and server code not run?
The scenario should work, but I can't repro your problem. May I see you sample code? Is it a VS project or just a plain single ASPX page?

Thanks
Yugang

The following code snippet works for me. If you use code behind model, you need to paste client script into aspx file and move server script code to code behind file

<%@. Page Language="VB" %>
<script runat="server">
Sub Page_Load()
if not Page.ispostback then
button1.attributes.add("onclick", "Javascript: return test();")
end if
end sub
Sub button_click(byval sender as object, byval e as System.EventArgs)
REsponse.write("<h1>post back</h1>")
end sub
</script>
<script language="javascript">
function test() {
alert("hi!");
}
</script>
<html>
<body>
<form id="form1" runat=server>
<asp:button runat=server id=button1 text="button1" name=button1 onclick="button_click"></asp:button>
</form>
</body>
</html>

0 comments:

Post a Comment