我不会在任何事情上使用主内容页面。我有一个简单的aspx页面。 仍然显示
名称' txtFirstName'在当前上下文中不存在
我的MarkUp
<%@ 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 txtFirstName_TextChanged(object sender, EventArgs e)
{
string firstName = txtFirstName.Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtFirstName" runat="server" OnTextChanged="txtFirstName_TextChanged"></asp:TextBox>
</form>
</body>
</html>
即使是OnTextChanged事件也正常启动。我不明白,可能是什么问题?
我搜索过的其他类似帖子并没有多大帮助。
请建议.....
答案 0 :(得分:3)
您必须拥有form
标记{/ 1}}
runat="Server"
注意: - 1)将脚本放在<form id="form1" runat="server">
<asp:TextBox ID="txtFirstName" runat="server" text="" AutoPostBack="True"/>
</form>
protected void txtFirstName_TextChanged(object sender, EventArgs e)
{
string firstName = txtFirstName.Text;
}
标记内..
<head>
答案 1 :(得分:0)
尝试
<form runat="server">
<asp:TextBox ID="txtFirstName" runat="server" text="" />
</form>
http://www.w3schools.com/aspnet/aspnet_textbox.asp
protected void txtFirstName_TextChanged(object sender, EventArgs e)
{
Button btn = sender as Button;
string firstName = btn.Text;
}