[英]textbox ontextchanged event is firing in update panel but throws error on client side
我在页面上有两个div。 首次加载页面时,它会显示带有2个单选按钮的第一个div。 如果选择第二个按钮,则它会回发并隐藏第一个div并显示第二个div。 在更新面板中具有文本框和下拉菜单。 将文本插入文本框并单击选项卡将触发OnTextChanged事件。 我在下拉菜单中添加选项。 但它会在客户端抛出错误。
error : "Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed."
不知道在这里做什么。
这是我的aspx页面。
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div id="divpanel1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label : "></asp:Label>
<asp:RadioButton ID="RadioButton1" GroupName="lbl" Text="me first" runat="server"/>
<asp:RadioButton ID="RadioButton2" GroupName="lbl" Text="me second"
runat="server" AutoPostBack="true" OnCheckedChanged="RadioButton2_CheckedChanged" />
</div>
<div id="divpanel" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Selected="True" Value="0">select</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
这是我的代码。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.divpanel.Visible = false;
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
this.DropDownList1.Items.Add(this.TextBox1.Text.ToString());
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
this.divpanel.Visible = true;
this.divpanel1.Visible = false;
}
}
感谢您的时间。
错误是您需要在更新面板中放入<div />
和<asp:TextBox /> <asp:DropDownList>
,因为控件文本框执行事件OntextChanged
它无法更新控件,并且不能执行此操作。工作。
下面的aspx的新代码:
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="divpanel1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label : "></asp:Label>
<asp:RadioButton ID="RadioButton1" GroupName="lbl" Text="me first" runat="server" />
<asp:RadioButton ID="RadioButton2" GroupName="lbl" Text="me second" runat="server"
AutoPostBack="true" OnCheckedChanged="RadioButton2_CheckedChanged" />
</div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="divpanel" runat="server">
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Selected="True" Value="0">select</asp:ListItem>
</asp:DropDownList>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
下面的新代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.divpanel.Visible = false;
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
this.DropDownList1.Items.Add(this.TextBox1.Text.ToString());
DropDownList1.DataBind();
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
this.divpanel.Visible = true;
this.divpanel1.Visible = false;
}
希望对您有所帮助。
干杯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.