Johny Bravo
asked on
bind Listbox in asp.net
Hi Experts,
I am binding a ListBox using below code,
private void FillRole()
{
SkillUpdateRight skUpdateRights = new SkillUpdateRight();
Dictionary<object, object> dictRole = skUpdateRights.GetAllRole( );
lstRole.DataSource = dictRole;
lstRole.DataTextField = "Value";
lstRole.DataValueField = "Key";
lstRole.DataBind();
}
skUpdateRights.GetAllRole( )
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;
}
Now I have a sql ,
Select RoleId, RoleName, 'false' AS RoleSelected from Role
UNION ALL
Select RoleId, RoleName, 'true' AS RoleSelected from Role
WHERE RoleID IN
(Select lr.RoleId from SkillUpdateRights lr
inner join Role r on r.RoleId = lr.RoleId)
I want to check the RoleSelected is true or false and accordingly bind the ListBox.
If the RoleSelected is true,I will set the item in the list as Selected otherwise it will just appear in the Listbox.
How can I acheive this functionality.
I am binding a ListBox using below code,
private void FillRole()
{
SkillUpdateRight skUpdateRights = new SkillUpdateRight();
Dictionary<object, object> dictRole = skUpdateRights.GetAllRole(
lstRole.DataSource = dictRole;
lstRole.DataTextField = "Value";
lstRole.DataValueField = "Key";
lstRole.DataBind();
}
skUpdateRights.GetAllRole(
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;
}
Now I have a sql ,
Select RoleId, RoleName, 'false' AS RoleSelected from Role
UNION ALL
Select RoleId, RoleName, 'true' AS RoleSelected from Role
WHERE RoleID IN
(Select lr.RoleId from SkillUpdateRights lr
inner join Role r on r.RoleId = lr.RoleId)
I want to check the RoleSelected is true or false and accordingly bind the ListBox.
If the RoleSelected is true,I will set the item in the list as Selected otherwise it will just appear in the Listbox.
How can I acheive this functionality.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the reply.
I want unique RoleID,Rolename.
If it is selected ,them true otherwise false.
Only true or false,not both
RoleID RoleName
1 TestRole 'true'
One more problem is that,how to bind this to Listbox depending on the True,false.
Currently I found one way to set the selected value in the Listbox.It directly set the selected property as true.
Which I want dynamic.I mean accrding to value the query returns (true/false)
SkillUpdateRight skUpdateRights = new SkillUpdateRight();
Dictionary<object, object> dictRoleSelected = skUpdateRights.GetSelected Role();
foreach (KeyValuePair<object, object> kvp in dictRoleSelected)
{
ListItem li = new ListItem(kvp.Value.ToStrin g(), kvp.Key.ToString());
li.Selected = true;
lstRole.Items.Add(li);
}
I want unique RoleID,Rolename.
If it is selected ,them true otherwise false.
Only true or false,not both
RoleID RoleName
1 TestRole 'true'
One more problem is that,how to bind this to Listbox depending on the True,false.
Currently I found one way to set the selected value in the Listbox.It directly set the selected property as true.
Which I want dynamic.I mean accrding to value the query returns (true/false)
SkillUpdateRight skUpdateRights = new SkillUpdateRight();
Dictionary<object, object> dictRoleSelected = skUpdateRights.GetSelected
foreach (KeyValuePair<object, object> kvp in dictRoleSelected)
{
ListItem li = new ListItem(kvp.Value.ToStrin
li.Selected = true;
lstRole.Items.Add(li);
}
ASKER
Thanks for the suggestion
In your SQL, you could create a third column "IsRoleSelected" as a bit data type for example and use the value of this column (0 or 1) to determine what to do each row of your Role table.