如何保持列表框的选定项处于选中状态 [英] how to keep the selected item of listbox selected
问题描述
我有一个列表框,其中三个列表项已经过硬编码.
Hi,
I have a listbox in which three list items have been hard coded.
<td align="justify">
<asp:ListBox ID="ListBox1" Width="200" SelectionMode="Multiple" runat="server" >
<asp:ListItem Text= "SP" Value= "1">
<asp:ListItem Text= "SC" Value= "2" >
<asp:ListItem Text= "TA" Value= "4" >
</td>
我打算再次检查所选项目时保持选中状态(突出显示).
请指导如何完成.
I intend to keep the selected item selected(highlighted) when i check it again.
Please guide how can it be done.
推荐答案
<asp:ListItem Text="SP" Value= "1" Selected="true" ></asp:ListItem>
更新:
离开页面之前,您可以将所选值存储在会话中.
Update :
Before leaving the page, you can store your selected value in a Session.
Session["SelectedListItem"] = ListBox1.SelectedValue;
在您的PageLoad上,您可以检查Session["SelectedListItem"]
是否具有值并进行设置.
And on your PageLoad, you can check the Session["SelectedListItem"]
if it has value and set it.
if (Session["SelectedListItem"] != null) {
ListBox1.SelectedValue = (string)Session["SelectedListItem"];
}
我不是ASPX专家,但是...我认为您需要将信息存储在会话中,并在此页面的Page_Load处理程序中设置适当的项目再次选择.请记住,默认情况下,HTTP是无状态的,因此每次访问该页面都是一次新访问,除非您编写一些会话代码来记住事情.
I''m not an ASPX expert but ... I think you will need to store the information in the session, and in your Page_Load handler for this page, set the appropriate items to be selected again. Remember that HTTP is by default stateless and so each visit to the page is a new visit, unless you write some session code to remember things.
ListBox.Items.Add(New ListItem("Select ListBox Type", ""))
ListBox.DataSource = object
ListBox.DataValueField = "TextField"
ListBox.DataTextField = "ValueField"
ListBox.DataBind()
ListBox.SelectedIndex = 0
在这里它默认显示为选择列表框类型",bcz的SelectedIndex设置为ZERO(0).
您可以根据需要进行更改.
Here it Shows by Default selection as "Select ListBox Type" bcz it''s SelectedIndex is set to ZERO(0).
You can change as per your requirement.
这篇关于如何保持列表框的选定项处于选中状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!