Hi Gary,
Thanks for posting in the community!
From your description, you use a clientside script to do some validation
operations on some certain entry fields on a web form. And after the
valication , you 'd like to post back the page via javascript code,yes?
If there is anything I misunderstood, please feel free to let me know.
As for this question, I think there are two generic approachs we can use:
1. using the "form.submit()" method to post back the whole page form.
For example, the page form is named as below in page:
<form id="Form1"....>
Then we can use the below javascript to post back the form.
document.Form1.submit();
After that, you can do some operations in the page's Page_Load handler in
codebehind.
2. adding a runat=server htmlinputbutton in the page and set its visible as
false, and use javascript to fire its click event. For example, we have
such a button in page:
<input type="button" id="btnPostBack" style="display:none" runat="server"
value="Post Back">
then, in your javascript block , call the following code to fire the
button's click event:
document.all("btnPostBack").click();
Also, since we set it as "runat=server", we can also register a server
click event handler in it (in code behind) and do serverside operations in
that handler, how do you think of this?
In addition, here is a simple test page I used to show the #2 means above,
you may have a view if you feel anything unclear:
-----------------------------aspx page----------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language=javascript>
function checkField()
{
if(document.all("txtMain").value.length>4)
{
document.all("btnPostBack").click();
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td><INPUT id="txtMain" type="text" runat="server"
onchange="checkField()" ></td>
</tr>
<tr>
<td><INPUT id="btnPostBack" style="DISPLAY: none" type="button"
value="PostBack" runat="server"></td>
</tr>
</table>
</form>
</body>
</HTML>
-----------------------code behind class--------------------------
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputText txtMain;
protected System.Web.UI.HtmlControls.HtmlInputButton btnPostBack;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.btnPostBack.ServerClick += new
System.EventHandler(this.btnPostBack_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnPostBack_ServerClick(object sender, System.EventArgs e)
{
Response.Write("<br>Page is posted back at: " +
DateTime.Now.ToLongTimeString());
}
}
----------------------------------------------------
Please check out my suggestions. If you have any questions on them or need
any further assistance, please feel free to post here.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)