RE: Creating a list of checkboxes dynamically
- From: brians[MCSD] <briansMCSD@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 2 Mar 2007 10:15:57 -0800
Hello Middletree,
I do something quite similar on one of my websites. I use a CheckBoxList and
populate it with a table from SQL database. The data binding code in ASP.NET
makes it pretty straightforward:
// Get a SqlDataReader from the datalayer - basically a rowset from SQL
cblCategories.DataSource = categories.SelectCategories();
// Set the DataTextField and the DataValueField's
cblCategories.DataTextField = "Category";
cblCategories.DataValueField = "CategoryID";
// DataBind the CheckBoxList to the rowset of categories
cblCategories.DataBind();
When you want to read the CheckBoxList you can do something like this:
for (int i = 0; i < cblCategories.Items.Count; i++)
{
if (cblCategories.Items[i].Selected)
{
// Do some processing
}
}
Obviously, this is C#. You'll need to figure out the VB.NET code, but it
shouldn't be difficult.
--
enjoy - brians
http://www.limbertech.com
"Middletree" wrote:
I'm a Classic ASP guy trying to learn .NET. I'm finding that it is making.
simple things harder, at least so far.
I'm trying to re-create, from scratch, a web-based app that I did a few
years back. The old one is located at
http://www.gracearlington.com/shape/shape/
If you open the page, you'll see there are 3 or 4 sections of checkboxes.
Each of them is from a table. For example, the table called Gifts has two
columns: a numeric one called ID, and a character one called Description. To
make this section appear in ASP, I simple did this(pseudocode follows):
<tr>
<%
Do a SQL Select to get the ID and Description from the Gift table
strTempRow = 0 ----this variable is used to make it start a new <TR> after
every other <TD>
While Not RS.EOF
Response.Write "<td colspan=2><input type=checkbox name=Gift
value='"&RS.Fields("GiftID")&"'>"&RS.Fields("GiftDesc")&"</td>"
strTempRow = strTempRow + 1
If strTempRow mod 2 = 0 then%>
</tr><tr>
<%
End if
RS.MoveNext
%>
======================================================================
That is some really simple code, yet I cannot find a way to do the same
thing in .NET. I am using VB for language. I have copies of VWD and Studio
2005. Using SQL Server Express 2005 (for now).
I'm not asking anyone to write code for me, but I am asking for guidance. I
am not getting how to do this at all.
- References:
- Creating a list of checkboxes dynamically
- From: Middletree
- Creating a list of checkboxes dynamically
- Prev by Date: Re: Https and images and js files
- Next by Date: Re: Creating a list of checkboxes dynamically
- Previous by thread: Re: Creating a list of checkboxes dynamically
- Next by thread: Re: Creating a list of checkboxes dynamically
- Index(es):
Relevant Pages
|