【问题标题】:textbox ontextchanged event is firing in update panel but throws error on client side文本框 ontextchanged 事件在更新面板中触发,但在客户端引发错误
【发布时间】:2013-05-09 04:39:35
【问题描述】:

我在页面上有两个 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;
   }
 }

感谢您的宝贵时间。

【问题讨论】:

    标签: c# asp.net ajax textbox updatepanel


    【解决方案1】:

    错误是你需要在更新面板中放入&lt;div /&gt;&lt;asp:TextBox /&gt; &lt;asp:DropDownList&gt;,因为当控件文本框执行事件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;
    
    }
    

    希望对你有所帮助。

    干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 2012-01-31
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多