Req.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Req.aspx.cs" Inherits="Req" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>HTML表单提交测试-Mzwu.Com</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Text1" type="text" />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>Req.aspx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class Req : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Request.Form["Text1"]);
}
}怎么提交,Request.Form["Text1"]始终没有值。后来发现,给Text1加上runat="server",就能正常获取值了-_-,今天在同事的提醒下,发现原来VS默认没有给控件添加name属性,添加上去就正常了:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Req.aspx.cs" Inherits="Req" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>HTML表单提交测试-Mzwu.Com</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Text1" type="text" [color=Red]name="Text1"[/color] />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>那为什么加上runat="server"也可以呢?查看了下网页源代码,发现加了runat="server"后,在生成的源代码中自动加上了name属性!
评论(0)
暂无评论。