Link to home
Start Free TrialLog in
Avatar of Johny Bravo
Johny Bravo

asked on

Jquery plugin(selection) in asp.net (How to show selected values from sql table in ListBox)

Hi Experts
I am using a Jquery plugin for selection.

Here is the link.
http://michael.github.com/multiselect/index.html?countries%5B%5D=AFG&countries%5B%5D=DZA&countries%5B%5D=ARG&countries%5B%5D=ABW&countries%5B%5D=AUT&countries%5B%5D=BGD

Now what I have done is instead of Select in Html, I have putted a ListBox,set its selectiomode 'Multiple '
and binded it with db.(As suggested by one of the Expert)

 <asp:ListBox ID="lstRole" class="multiselect" multiple="multiple" SelectionMode="Multiple" runat="server"></asp:ListBox>
                               

private void FillRole()
        {
            AccessRights accessRights = new AccessRights();
            Dictionary<object, object> dictRole = accessRights.GetAllRole();
            lstRole.DataSource = dictRole;
            lstRole.DataTextField = "Value";
            lstRole.DataValueField = "Key";
            lstRole.DataBind();
         
        }


On Save Button,
I am getting the selected values.
protected void imgbtnSave_Command(object sender, CommandEventArgs e)
        {
            List<string> selectedItems = new List<string>();
            foreach (ListItem item in lstRole.Items)
            {
                if (item.Selected)
                    selectedItems.Add(item.Value);
            }

            string allSelectedItems = String.Join(",", selectedItems.ToArray());
        }


I have stored these values in a seperate table.And  now I want to fetch them and show in the listbox as already selected.

Please let me know if I miss any explanation.

Your help is very much appreciated.
Avatar of Albert Van Halen
Albert Van Halen
Flag of Netherlands image


private void FillRole()
{
    Dictionary<object, object> dictRole = new AccessRights().GetAllRole();
    foreach (KeyValuePair<object, object> kvp in dictRole)
    {
        ListItem li = new ListItem(kvp.Key.ToString(), kvp.Value.ToString());
        li.Selected = true;
        lstRole.Items.Add(li);
    }
}

Open in new window

Avatar of Johny Bravo
Johny Bravo

ASKER

I think I have not explained it well.
I have a table 'Role'
Currently I am getting Roles(Listbox values) as ,'Select RoleId,RoleName from Roles'
Now in the form I am saving the values in a different table(values which user selects in the ListBox)
The table is,LevelRights
Now the table 'LevelRights' contains  RoleId in it.

Select lr.RoleId,r.RoleName from LevelRights lr
inner join Roles r on r.RoleId = lr.RoleId

"The output of this statement,I need to show as seletecd."
I mean the RoleId present in LevelRights,I want to show them as selected(And I don't want those RoleId in the Available List)
So you have two selectboxes : One with all the available roles (excluding the ones selected) and the other with all the selected roles?
You could modify your sql to select only the roles that are left for selection.

Select RoleId,RoleName from Roles WHERE RoleID NOT IN (Select Select lr.RoleId from LevelRights lr
inner join Roles r on r.RoleId = lr.RoleId)

The available roles are in the first selectbox (you can do that via simple datasource binding providing the SQL statement above).
The selected roles are in the second selectbox, all of them are selected via the method I posted earlier.
well AlbertVanHalen,they are not two textboxes.
Well it is the JQuery plugin,it is the ListBox control a single one.
but with the help of JQuery it is divided into two.
One part as 'Available list'(Like normal Listbox only) and other part as 'Selected List',user adds the item from 'Available' to 'Seleted'
('Available' n 'Selected' are the terms used for understanding,not actual ctrl names)
Pls see the screens
scrn1.JPG
scr2.JPG
ASKER CERTIFIED SOLUTION
Avatar of Albert Van Halen
Albert Van Halen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmm got your point,
Currently I m using Dictionary object,What should I need to change
 public Dictionary<object, object> GetAllRole()
        {
            Dictionary<object, object> DictRole = new Dictionary<object, object>();
            //IDataReader dr = DBDataHandler.ExecuteReader(strCon, WDProcedures.GET_ALL_RESOURCES, null);

            IDataReader dr = DBDataHandler.ExecuteReader(strCon, WDProcedures.GET_ROLESFORSKILLRIGHTS, null);
            while (dr.Read())
            {
                if (DictRole == null)
                {
                    DictRole = new Dictionary<object, object>();
                    DictRole.Add(dr["RoleId"].ToString(), dr["RoleName"].ToString());
                }
                else
                {
                    if (!DictRole.ContainsKey(dr["RoleId"].ToString()))
                    {
                        DictRole.Add(dr["RoleId"].ToString(), dr["RoleName"].ToString());
                    }
                }
            }
            dr.Close();

            return DictRole;
        }
For binding ListBox
{
SkillUpdateRight skUpdateRights = new SkillUpdateRight();
            Dictionary<object, object> dictRole = skUpdateRights.GetAllRole();
            lstRole.DataSource = dictRole;
            lstRole.DataTextField = "Value";
            lstRole.DataValueField = "Key";
            lstRole.DataBind();
}