日期:2014-05-19  浏览次数:20447 次

ASP.NET 的ListBox问题
我打算单击ListBox中一项后会在同一页面上的另一个文本框里显示这一项的text,     能使用ListBox的SelectedIndexChanged   事件吗?   要让这个事件起作用要把autoPostBack设为true,   可页面一刷新后,ListBox中选中的项没了```  


 


------解决方案--------------------
aspx:
<asp:ListBox ID= "ListBox1 " runat= "server " AutoPostBack= "True " OnSelectedIndexChanged= "ListBox1_SelectedIndexChanged "> </asp:ListBox>
<asp:TextBox ID= "TextBox1 " runat= "server "> </asp:TextBox>

cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.Items.Add(new ListItem( "1 ", "1 "));
ListBox1.Items.Add(new ListItem( "2 ", "2 "));
ListBox1.Items.Add(new ListItem( "3 ", "3 "));
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.TextBox1.Text = ListBox1.SelectedItem.Text;
}