Click here to Skip to main content
15,070,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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



C#
<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>



C#
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);
            }
        }
    }
}
Posted
Updated 18-Oct-11 22:20pm
v2

Is the ViewState 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

C#
Session["ddlAdminValue"] = ddlAdmin.SelectedIndex;


On PageLoad() you can do something like;
C#
if(Page.IsPostBack)
{
  if(Session["ddlAdminValue"] != null)
  {
    ddlAdmin.SelectedIndex = Convert.ToInt32(Session["ddlAdminValue"]);
   } 
}
   
not sure, but in most cases, filling drop down on if !postback in pageload will save from reseting the values of it.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900