I'm working on a ASP.NET app in VB.Net that will display a list of Warehouses. By default any warehoses that the selected user has access to should be checked.
My CheckBoxList control is populated with a DataTable which was filled using the following SQL Query
The "Name" field is used in the DataTextField property, the 'whsenum" field is used in the DataValueField property and the "selected" field will be a flag to determine if a checkbox needs to be checked by default or not. (If the Selected value is not equal
to 0 then that means that there is a record for the user/warehosue combination in the users_whse table and that the checkbox should be checked by default).
I know I'll probably need to write a routine to check for the value being returned from the "selected" field and set the checked properety appropriatly. but I'm kinda stuck on how to go about doing this effectively.
If anyone has any advice or can point me in the right direction I would be very appreciative.
You can achive that in the DataBound Event of the CheckBoxList ..
1.) First in the Query club the whesnum and selected field into one column ..
2.) select (wm.whsenum + '/' + ISNULL(usrwhse.users_whseid, 0) ) as whsenum and set that as the DataValueField...
3.) Create the DataBoundEvent for the CheckBoxList
protected void DataBoundMy(object sender, EventArgs e)
{
for (int i = 0; i < chkboxlst.Items.Count; i++)
{
string a = chkboxlst.Items[i].Value;
string[] vals = a.Split('/');
if( Convert.ToInt32(vals[1]) == 0)
{
chkboxlst.Items[i].Selected = true;
}
}
}
When you run this code it will check for the selected value of each Item and check the box based on the condition provided...
This would work nomally but I'll be using the whsenum when the user hits the set warehosues button to update the database via a stored procedure. I guess I could eliminate anything after the "/" and pass that to the parameter of the stored proceduure. I'll
give it a try tonight and let you all know how I make out,.
For i = 0 To dtDataTable.Rows.Count - 1
chkboxlist1.Items.FindByValue(dtDataTable.Rows(i).Item ("Name")).Selected = True
Next
see this very nice article with code:
Thanks, That worked with a little modification of my own. I needed to only have the checkbox selected for the warehouses that had a value in the "selected" column of my data Table.
Below is the code that I added that works.
Using adapter As New SqlDataAdapter(strSql, cnn)
adapter.Fill(dt)
cblUserWarehouses.DataSource = dt
cblUserWarehouses.DataBind()
For i = 0 To dt.Rows.Count - 1
If dt.Rows(i).Item("selected") > 0 Then
cblUserWarehouses.Items.FindByValue(dt.Rows(i).Item("whsenum")).Selected = True
End If
Next
Marked as answer by Deano252 on Feb 24, 2012 01:42 PM
Deano252
Member
24 Points
9 Posts
Setting the Checked Property of CheckBoxList Items based on a value from the Database.
Feb 23, 2012 08:01 PM|LINK
Hi all,
I'm working on a ASP.NET app in VB.Net that will display a list of Warehouses. By default any warehoses that the selected user has access to should be checked.
My CheckBoxList control is populated with a DataTable which was filled using the following SQL Query
strSql = "select wm.whsenum as whsenum,u.user_num,wm.fsl + ' - ' + wm.name as Name, " & _ "ISNULL(usrwhse.users_whseid, 0) as selected " & _ "from whse_mst wm " & _ "cross join users u " & _ "left outer join " & _ "(select uw.whsenum, uw.user_num, uw.users_whseid from users_whse uw) usrwhse " & _ "on wm.whsenum = usrwhse.whsenum and u.user_num = usrwhse.user_num " & _ "where u.user_num = " & strUsernum & " and wm.activity_code = 'A'"The "Name" field is used in the DataTextField property, the 'whsenum" field is used in the DataValueField property and the "selected" field will be a flag to determine if a checkbox needs to be checked by default or not. (If the Selected value is not equal to 0 then that means that there is a record for the user/warehosue combination in the users_whse table and that the checkbox should be checked by default).
I know I'll probably need to write a routine to check for the value being returned from the "selected" field and set the checked properety appropriatly. but I'm kinda stuck on how to go about doing this effectively.
If anyone has any advice or can point me in the right direction I would be very appreciative.
Thank,
Dean
rajsedhain
Contributor
4181 Points
1041 Posts
Re: Setting the Checked Property of CheckBoxList Items based on a value from the Database.
Feb 23, 2012 08:07 PM|LINK
do something like this:
<asp:CheckBox ID="chk1" runat="server" Checked='<%# bool.Parse(Eval("selected").ToString()) %>'/>more:
http://stackoverflow.com/questions/6445178/set-checkbox-in-gridview-based-on-datatable-value
Raj Sedhain
Deano252
Member
24 Points
9 Posts
Re: Setting the Checked Property of CheckBoxList Items based on a value from the Database.
Feb 23, 2012 08:15 PM|LINK
The control is a CheckBoxList Control and not a single checkbox control. There is no property in the CheckBoxList Control for Checked
Would this be something that would have to be done on the OnBinding Event?
Thanks,
Dean
rajsedhain
Contributor
4181 Points
1041 Posts
Re: Setting the Checked Property of CheckBoxList Items based on a value from the Database.
Feb 23, 2012 08:21 PM|LINK
then you can have coding something like this:
For i = 0 To dtDataTable.Rows.Count - 1 chkboxlist1.Items.FindByValue(dtDataTable.Rows(i).Item ("Name")).Selected = True Nextsee this very nice article with code:
http://www.4guysfromrolla.com/articles/050703-1.aspx
Raj Sedhain
sushanth009
Contributor
6243 Points
1168 Posts
Re: Setting the Checked Property of CheckBoxList Items based on a value from the Database.
Feb 23, 2012 08:47 PM|LINK
You can achive that in the DataBound Event of the CheckBoxList ..
1.) First in the Query club the whesnum and selected field into one column .. 2.) select (wm.whsenum + '/' + ISNULL(usrwhse.users_whseid, 0) ) as whsenum and set that as the DataValueField... 3.) Create the DataBoundEvent for the CheckBoxList protected void DataBoundMy(object sender, EventArgs e) { for (int i = 0; i < chkboxlst.Items.Count; i++) { string a = chkboxlst.Items[i].Value; string[] vals = a.Split('/'); if( Convert.ToInt32(vals[1]) == 0) { chkboxlst.Items[i].Selected = true; } } }When you run this code it will check for the selected value of each Item and check the box based on the condition provided...
Deano252
Member
24 Points
9 Posts
Re: Setting the Checked Property of CheckBoxList Items based on a value from the Database.
Feb 23, 2012 08:56 PM|LINK
This would work nomally but I'll be using the whsenum when the user hits the set warehosues button to update the database via a stored procedure. I guess I could eliminate anything after the "/" and pass that to the parameter of the stored proceduure. I'll give it a try tonight and let you all know how I make out,.
Thanks for all the help.
Deano252
Member
24 Points
9 Posts
Re: Setting the Checked Property of CheckBoxList Items based on a value from the Database.
Feb 24, 2012 01:41 PM|LINK
Thanks, That worked with a little modification of my own. I needed to only have the checkbox selected for the warehouses that had a value in the "selected" column of my data Table.
Below is the code that I added that works.
Using adapter As New SqlDataAdapter(strSql, cnn) adapter.Fill(dt) cblUserWarehouses.DataSource = dt cblUserWarehouses.DataBind() For i = 0 To dt.Rows.Count - 1 If dt.Rows(i).Item("selected") > 0 Then cblUserWarehouses.Items.FindByValue(dt.Rows(i).Item("whsenum")).Selected = True End If Next