如何保留asp.net中dropdownlistbox的值 [英] How to retain the value of dropdownlistbox in asp.net
问题描述
在这种情况下,我有1个下拉列表用于选择用户,还有2个列表框,用于为该用户设置项目权限.现在,我希望首先选择下拉列表,然后单击移动"按钮,将listbox1的listitem移至listitem2.然后单击提交",将它们添加到表中.但是当我自动回传下拉列表和使用更新面板时,下拉列表的选定索引消失了.单击按钮移动时,其选择的值消失.那么我该如何保留其价值
In this i have 1 dropdown which selects the user and 2 list box which sets the permission of items for that user . Now i want that firstly dropdown i selected and then on click of move button the listitem of listbox1 move to listitem2 . And then on click of submit they added in table . But the selected index disappear of dropdown list when i autopostback it and when i am using the update panel . Its selected value disappear when the button move clicked . So how can i retain its value
<div>
<asp:DropDownList ID="ddlAdmin" runat="server" >
<table width="70%">
<tr><td colspan="3">
<asp:ListBox ID="ProductListBox" runat="server" SelectionMode="Multiple" ></td>
<td><asp:Button ID="BtnMoveLeft" runat="server" Text="Add Permission" OnClick="BtnMoveleft_Click" /></td>
<td>
<asp:ListBox ID="PermissionListBox" runat="server" SelectionMode="Multiple" ></td>
</tr>
</table>
</div>
public partial class admin_UserPermission : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillProductList();
FillAdminDropDownList();
}
protected void FillAdminDropDownList()
{
DataManager myData = new DataManager(DbFunctions.GetConnectionString());
MySqlConnection myPConn = myData.GetPooledConnection(new MySqlConnection());
MySqlCommand cmd = new MySqlCommand("SELECT adminuserid,adminusername FROM adminusermast", myPConn);
DataTable dt = new DataTable();
myPConn.Open();
MySqlDataAdapter adptr = new MySqlDataAdapter(cmd);
adptr.Fill(dt);
myPConn.Close();
if (dt.Rows.Count > 0)
{
ddlAdmin.Items.Clear();
ddlAdmin.Items.Add("SELECT");
foreach (DataRow drow in dt.Rows)
{
ListItem lst = new ListItem();
lst.Value = drow[1].ToString();
lst.Text = drow[0].ToString();
ddlAdmin.Items.Add(lst);
}
}
}
protected void FillProductList()
{
DataManager myData = new DataManager(DbFunctions.GetConnectionString());
MySqlConnection myPConn = myData.GetPooledConnection(new MySqlConnection());
MySqlCommand com = new MySqlCommand("Select productid,productname from productmast", myPConn);
MySqlDataReader Reader = null;
myPConn.Open();
Reader = com.ExecuteReader();
while (Reader.Read())
{
ListItem newItem = new ListItem();
newItem.Text = Reader["productname"].ToString();
newItem.Value = Reader["productid"].ToString();
ProductListBox.Items.Add(newItem);
}
Reader.Close();
myPConn.Close();
}
protected void BtnMoveleft_Click(object sender, EventArgs e)
{
for (int i = ProductListBox.Items.Count - 1; i >= 0; i--)
{
if (ProductListBox.Items[i].Selected == true)
{
PermissionListBox.Items.Add(ProductListBox.Items[i]);
ListItem li = ProductListBox.Items[i];
ProductListBox.Items.Remove(li);
}
}
}
}
推荐答案
ViewState
属性是否已启用?如果未启用.
如果启用了此功能,则可以将所选索引保存在会话中,以后再重新分配.
OnSelectedindexchanged事件分配类似
Is theViewState
property is Enabled? If not enable it.
If it is enabled you can save the selected index in a Session and reassign it later.
OnSelectedindexchanged event assign something like
Session["ddlAdminValue"] = ddlAdmin.SelectedIndex;
在PageLoad()上,您可以执行类似的操作;
On PageLoad() you can do something like;
if(Page.IsPostBack)
{
if(Session["ddlAdminValue"] != null)
{
ddlAdmin.SelectedIndex = Convert.ToInt32(Session["ddlAdminValue"]);
}
}
不确定,但是在大多数情况下,如果页面加载中的!postback可以避免重置它的值,则填充下拉列表.
not sure, but in most cases, filling drop down on if !postback in pageload will save from reseting the values of it.
这篇关于如何保留asp.net中dropdownlistbox的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!