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:ListB ox>
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.Val ue);
}
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.
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:ListB
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.Val
}
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.
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)
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.
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.
ASKER
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)
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)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.ExecuteReade r(strCon, WDProcedures.GET_ALL_RESOU RCES, null);
IDataReader dr = DBDataHandler.ExecuteReade r(strCon, WDProcedures.GET_ROLESFORS KILLRIGHTS , 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;
}
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.ExecuteReade
IDataReader dr = DBDataHandler.ExecuteReade
while (dr.Read())
{
if (DictRole == null)
{
DictRole = new Dictionary<object, object>();
DictRole.Add(dr["RoleId"].
}
else
{
if (!DictRole.ContainsKey(dr[
{
DictRole.Add(dr["RoleId"].
}
}
}
dr.Close();
return DictRole;
}
ASKER
For binding ListBox
{
SkillUpdateRight skUpdateRights = new SkillUpdateRight();
Dictionary<object, object> dictRole = skUpdateRights.GetAllRole( );
lstRole.DataSource = dictRole;
lstRole.DataTextField = "Value";
lstRole.DataValueField = "Key";
lstRole.DataBind();
}
{
SkillUpdateRight skUpdateRights = new SkillUpdateRight();
Dictionary<object, object> dictRole = skUpdateRights.GetAllRole(
lstRole.DataSource = dictRole;
lstRole.DataTextField = "Value";
lstRole.DataValueField = "Key";
lstRole.DataBind();
}
Open in new window