4,如何强制将服务器控件放入<form runat=server></form>的标记之间
有些服务器控件可以不放在<form runat=server></form>的标记之间,如Label控件,但如果需要强制将它放<form runat=server></form>的标记之间,可以使用下面的方法:
protected void Label1_PreRender(object sender, EventArgs e)
{
this.VerifyRenderingInServerForm(Label1);
}
5,百害而无一益?
有时候,页面上需要放置多个form表单(虽然只放置一个<form runat=server></form>的表单也能实现),将表单控件放在<form runat=server></form>标记之外,将非常方便使用,这在以前的asp页面中很常见,现在在aspx中也可义实现。下面的页面,既利用了服务器控件的方便性,也逃脱出了类型“TextBox”的控件“TextBox1”必须放在具有 runat=server 的窗体标记内的限制。例如:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<li>TextBox1.Text = " + TextBox1.Text);
Response.Write("<li>Request.Params = " + Request.Params[TextBox1.UniqueID]);
}
protected void Page_Load(object sender, EventArgs e)
{
KeyWords.Text = "《ASP.NET2.0应用开发技术》";
Response.Write("<li>TextBox1.Text = " + TextBox1.Text);
Response.Write("<li>Request.Params = " + Request.Params[TextBox1.UniqueID]);
if (!IsPostBack)
{
TextBox1.Text = "《ASP.NET2.0应用开发技术》";
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>VerifyRenderingInServerForm</title>
</head>
<body>
<form method="post" action="SearchDoc.aspx">
关键字:<asp:TextBox ID="KeyWords" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="搜索" />
</form>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" Width="600px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="提交" />
</form>
</body>
</html>
在SearchDoc.aspx页面,使用Request.Form即可获得输入的关键字。

