Hi Al,
Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, you're looking for some information on how to post
back a page via client script(such as javasciprt) and also use some other
ways besides "form.submit()", such as call a server control's postback
event?
If there is anything I misunderstood, please feel free to let me know.
As for this problem, firstly I should confirm that Kevin has provided the
very good explanation on submit a form and the mechanism when doing work on
the page's post back events. Here I'll provide you some additional
suggestion:
In addition, we could also use javascript to call a servercontrol's
postback event. In fact, all the server controls such as an ASP.NET Button,
when it is output in the response page to client, it is rendered as a
normal html element, normally it'll be rendered as a <input type=submit />
or <input type=button /> , yes , just a html button. And as we know, in
javascript, we can manually fire a html element's DHTML event . For example
if there is a html button as:
<input type=button id="btnTest" value="click me to post back" />
Then, we can use the below javascript code to fire its click event:
btnTest.click();
And as for the problem in our issue, you can also use the above code to
fire a server controls's post back event. Here is a simple sample to show
this way:
--------------------------------------------aspx
file---------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>PostBack</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function doCheck()
{
if(document.all("txtPost").value.length > 4)
{
document.all("btnPost").click();
}
else
{
alert("input text is not enought!");
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td><INPUT id="txtPost" type="text" name="txtPost" runat="server"
onchange="doCheck()"></td>
<td><INPUT id="btnPost" type="button" value="Button" name="btnPost"
runat="server" style="display:none"></td>
</tr>
<tr>
<td><FONT face="ËÎÌå"></FONT></td>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>
--------------------------------------------------code-behind page
class------------------------------------------------------------
public class PostBack : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputButton btnPost;
protected System.Web.UI.HtmlControls.HtmlInputText txtPost;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnPost.ServerClick += new
System.EventHandler(this.btnPost_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnPost_ServerClick(object sender, System.EventArgs e)
{
Response.Write("<br>Page is post back at: "+
System.DateTime.Now.ToLongTimeString());
}
}
---------------------------------------------------------------------------
Please check out my suggestion to see whether it helps. If you have any
questions or need any 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.)