0

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

感谢您的时间。

4

1 回答 1

0

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

}

我希望有帮助。

干杯。

于 2013-05-08T20:39:35.840 回答