[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

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.
0
Johny Bravo
Asked:
Johny Bravo
  • 5
  • 3
1 Solution
 
Albert Van HalenAnalyst developerCommented:

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

0
 
Johny BravoAuthor Commented:
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)
0
 
Albert Van HalenAnalyst developerCommented:
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Johny BravoAuthor Commented:
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)
0
 
Johny BravoAuthor Commented:
Pls see the screens
scrn1.JPG
scr2.JPG
0
 
Albert Van HalenAnalyst developerCommented:
So you need ALL the roles (available and selected).
I would create a SQL statement wich will give you the result (via a union), loop through the result and fill the listbox with listitems and specify the selected attribute.
Select RoleId, RoleName, 'false' AS RoleSelected from Roles
UNION ALL
Select RoleId, RoleName, 'true' AS RoleSelected from Roles WHERE RoleID NOT IN (Select Select lr.RoleId from LevelRights lr inner join Roles r on r.RoleId = lr.RoleId)

Open in new window

0
 
Johny BravoAuthor Commented:
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;
        }
0
 
Johny BravoAuthor Commented:
For binding ListBox
{
SkillUpdateRight skUpdateRights = new SkillUpdateRight();
            Dictionary<object, object> dictRole = skUpdateRights.GetAllRole();
            lstRole.DataSource = dictRole;
            lstRole.DataTextField = "Value";
            lstRole.DataValueField = "Key";
            lstRole.DataBind();
}
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now