文本框ontextchanged事件在更新面板中触发,但在客户端抛出错误

时间:2013-05-08 19:54:27

标签: c# asp.net ajax textbox updatepanel

我在页面上有两个div。当第一次加载页面时,它显示第一个div,它有2个单选按钮。如果你选择第二个按钮它会回发并隐藏第一个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;
   }
 }

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

错误是您需要在更新面板中放置<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;

}

我希望有所帮助。

干杯。