<%@ 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 Page_Load(object sender, EventArgs e)
{
//得到TextBox1
string cname = Request.Form["__EVENTTARGET"];
//得到TextBox1的Text值
string cvalue = Request.Form["__EVENTARGUMENT"];
if (cname != null && cvalue != "")
{
this.Label1.Text = "控件名 " + cname + " 控件值 " + cvalue;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function submitTextBox1() {
var txtBox1 = document.getElementById("TextBox1");
__doPostBack("TextBox1", txtBox1.value);
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
通过javascript代码提交文本框内容到服务器<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
文本的内容:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:Button ID="Button1" runat="server" OnClientClick="return submitTextBox1();"
Text="doPostBack" />
<br />
<%--必须将dropdownList1 的autopostback属性设为 true 才能自动在页面上产生__doPostBack函数
并且在页面上产生两个隐藏的按钮
一个名为__EVENTTARGET 另一个名为__EVENTARGUMENT
第一个的值是触发事件的控件名
第二个的值是所带的参数--%>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Width="0px">
</asp:DropDownList>
</div>
</form>
</body>
</html>