簡體   English   中英

文本框ontextchanged事件在更新面板中觸發,但在客戶端拋出錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM