Creating a list of checkboxes dynamically

M

Middletree

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.
 
G

Guest

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.Selected)
{
// Do some processing
}
}

Obviously, this is C#. You'll need to figure out the VB.NET code, but it
shouldn't be difficult.
 
M

Middletree


I didn't really understand this page.
NO LOOPING WITH RESPONSE.WRITE

Thanks. Good to know.
Use Repeater , GridView , DataList (2.0).

I was trying to use the latter 2 before I posted this. Now I know about the
repeater, so I'll see what I can do with that.

I'll check it out. Thanks.
 
M

Mark Rae

I was not familiar with the Repeater control. Thanks!

No problem...

And I hope you won't take this the wrong way (you said yourself that you are
an ASP.NET newbie...) but getting hold of a beginner's guide to ASP.NET will
be of huge help to you... ASP.NET is very different from ASP, as you've no
doubt discovered... Until you have a grasp of the basics, you're going to
find it all very daunting and frustrating...

Something like this:
http://www.amazon.com/ASP-NET-All-R...0993755?ie=UTF8&s=books&qid=1172870659&sr=8-1

Alternatively, there are some excellent beginners' tutorials available on
the web...
 
M

Middletree

getting hold of a beginner's guide to ASP.NET will be of huge help to you.

Oh, yeah, I knew that, and in fact, I have been going through a couple of
books for newbies. I just hadn't seen anything that helps me with this
particular task in any of the books.

thanks
 
M

Mark Rae

Oh, yeah, I knew that, and in fact, I have been going through a couple of
books for newbies. I just hadn't seen anything that helps me with this
particular task in any of the books.

That's excellent! You obviously know what you need to do, and are doing it.
Your original request for assistance was clear and well phrased, so I'm sure
you'll be up to speed on ASP.NET in no time at all...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top