在UpdatePanel之外更改ListBox事件上的标签文本不起作用

本文关键字:标签 文本 不起作用 事件 ListBox UpdatePanel | 更新日期: 2023-09-27 17:57:45

我有下面的ListBox代码:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
   <asp:ListBox ID="ListBox1" runat="server" onselectedindexchanged="ListBox1_SelectedIndexChanged" AutoPostBack="True">
     <asp:ListItem Value="0">Item1</asp:ListItem>
     <asp:ListItem Value="1">Item2</asp:ListItem>
     <asp:ListItem Value="2">Item3</asp:ListItem>
   </asp:ListBox>
 </ContentTemplate>
</asp:UpdatePanel>
<br />
<asp:Label ID="lblTB" runat="server"/>

这是索引更改事件背后的代码:

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  lblTB.Text = "You choose: " + ListBox1.SelectedItem.Text;
}

我不想把控制标签放到UpdatePanel中。如何运行此代码?谢谢大家。

在UpdatePanel之外更改ListBox事件上的标签文本不起作用

代码看起来很好,只需添加ScriptManager。试试这个:

       <asp:ScriptManager runat="server"></asp:ScriptManager>
       <asp:UpdatePanel ID="UpdatePanel1" runat="server">
       <ContentTemplate>
       <asp:ListBox ID="ListBox1" runat="server"
onselectedindexchanged="ListBox1_SelectedIndexChanged" AutoPostBack="True">
       <asp:ListItem Value="0">Item1</asp:ListItem>
       <asp:ListItem Value="1">Item2</asp:ListItem>
       <asp:ListItem Value="2">Item3</asp:ListItem>
       </asp:ListBox>
       </ContentTemplate>
       </asp:UpdatePanel>
       <asp:Label ID="lblTB" runat="server"/>

后面的代码应该是.

   try
    { 
        HttpCookie getcook = Request.Cookies["saveme"];
        string check = getcook["saveme"].ToString();
        if (getcook["saveme"].Length > 0 &&      getcook["saveme"].ToString()!="none")
          {
             lblTB.Text = getcook["saveme"].ToString();
          }
        }
        catch(Exception er)
        {
            HttpCookie cookie = new HttpCookie("saveme");
            cookie["saveme"] = "none";
            Response.Cookies.Add(cookie);
        }
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblTB.Text = "You choose: " + ListBox1.SelectedItem.Text;
        HttpCookie setcook =Request.Cookies["saveme"];
        setcook["saveme"] = lblTB.Text;
        setcook.Expires = DateTime.Now.AddDays(1d); ;
        Response.Cookies.Add(setcook);
        Response.Redirect("updatepanelhere.aspx", false);
    }