RE: Creating a list of checkboxes dynamically



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.







.



Relevant Pages

  • BITs or INTEGERs? (J2EE EL related)
    ... Currently I have a fairly large SQL table. ... field that I'm using to represent 5 checkboxes on a JSP form. ... a static constant in my code for readability). ...
    (comp.lang.java.databases)
  • Help with search query
    ... available amenities in a home. ... when the user selects various checkboxes from this list ... how do I structure the SQL statement to do the search? ... should be displayed first, along with an indicator: Percentage Match: x%. ...
    (microsoft.public.sqlserver.programming)
  • Search Query Question
    ... available amenities in a home. ... when the user selects various checkboxes from this list ... how do I structure the SQL statement to do the search? ... should be displayed first, along with an indicator: Percentage Match: x%. ...
    (microsoft.public.sqlserver.programming)
  • Help with search query
    ... available amenities in a home. ... when the user selects various checkboxes from this list ... how do I structure the SQL statement to do the search? ... should be displayed first, along with an indicator: Percentage Match: x%. ...
    (microsoft.public.dotnet.framework.aspnet)