简体   繁体   中英

textbox ontextchanged event is firing in update panel but throws error on client side

i have two div on page. when first time page load it shows first div which has 2 radio button. if you select second button it does postback and hides first div and shows second div. which has textbox and dropdown inside update panel. insert text into textbox and hit tab it will fire OnTextChanged event. where i am adding option inside the dropdown. but it throws error on client side.

error : "Microsoft JScript runtime error:  Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed."

Not sure what to do here.

here is my aspx page.

<!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>

here is my code behind.

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;
   }
 }

Thank you for your time.

The error was that u need put in the update panel the <div /> and <asp:TextBox /> <asp:DropDownList> , Becouse when the control Textbox Execute the event OntextChanged It is not able to update the controls and that doesn't work.

The new code for the aspx below :

<!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>

The new codebehind below:

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;

}

I hope that help.

Cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM