Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Dynamic Non-Grouped Checkboxes

Author  Topic 

jhermiz

3564 Posts

Posted - 2005-02-02 : 12:56:10
I think it is possible just need some guidance on how to dynamically generate checkboxes in vb.net / asp.net and be able to reference them in code.

For instance, lets say a table has 3 employees Jon Jim and Todd. When my asp.net page loads I'd like to generate these 3 checkboxes, and I dont want these to be static because the number of employees changes. We may fire Jon tomorrow..or he may hit the lottery . We may add employees at a later time...etc.

I dont want them to be in one group because the end user should be able to check off any of the names to show more relevant data. Is there an article or example I can look at in VB.net / asp.net in doing this ?

Thanks,
Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]

jhermiz

3564 Posts

Posted - 2005-02-02 : 12:57:29
Also do I need to be concerned about the alignment of these boxes on a page or are they stored on some sort of container?

Edit:
And I need each box to have the events associated with it (Click, etc)

Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2005-02-02 : 13:23:57
Jon,
When I've done dynamic controls like this, I've always had to "hack" it to make it work. Basically, I create a very special ID for each control, so that they stand out from any other web form control. Upon postback, I loop through the request.form collection, find my "special" controls and handle them as needed. It's not very easy to work with, but it is dynamic.

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-02-02 : 13:28:15
quote:
Originally posted by MichaelP

Jon,
When I've done dynamic controls like this, I've always had to "hack" it to make it work. Basically, I create a very special ID for each control, so that they stand out from any other web form control. Upon postback, I loop through the request.form collection, find my "special" controls and handle them as needed. It's not very easy to work with, but it is dynamic.

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>



Hey Mike,

Can you provide more info? For instance, whe nyou say special ID..how are you giving it an ID is there an attribute you can use dynamically. Also, how does that allow the code behind to trigger an event for that? Also how do you handle placement, are you actually generating the check box on the page dynamically or do you place a static one and set its visible property ?

Surely there has to be an example somewhere :)

Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-02-02 : 13:54:32
Just add a panel control to your webpage, place it where it needs to be postioned, and using code enumerate your values and fill it up with CheckBox controls when you build your page.

i.e., something like this:

(this just adds 10 checkboxes, but you could open a DataReader and use the exact same technique)


Dim i As Integer
Dim chk As Web.UI.WebControls.CheckBox

For i = 1 To 10
With Panel1
chk = New CheckBox
chk.Text = "Value #" & i
chk.ID = "Check" & i
chk.Checked = False
.Controls.Add(chk)
.Controls.Add(New LiteralControl("<BR>"))
End With
Next


Then, all you need to do on some server-side event is enumerate the controls of your panel and see if they are checked:


Dim c As Control
For Each c In Panel1.Controls
If Left(c.ID, 5) = "Check" Then
If DirectCast(c, CheckBox).Checked Then
Response.Write(c.ID & " is checked!<BR>")
End If
End If
Next


something like that ... you get the idea. Use panels to group controls and it makes it quite easy to see what is going on.

(FYI -- the code above will actually compile + work if the first part is on the Page_Load() event and the second part is on a Button_Click() event, and you have a control named "Panel1" on your page)

- Jeff
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2005-02-02 : 13:56:10
This article may be useful:

http://www.dotnetspider.com/technology/kbpages/1045.aspx
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-02-02 : 13:59:11
(I like my example better!)





- Jeff
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2005-02-02 : 14:02:52
It is a very nice example :)

Jon, is this something you will be doing in a datagrid or other type of repeating control?
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2005-02-02 : 14:17:03
What Jeff has is pretty much exactly how mine works. The only difference is for my control ID, I store data that allows me to know what entity was checked.
With your example, I'd put the employeeID in as a part of the CheckBox controls' ID. Using Jeff's Example:

chk.ID = "Check" & i & "|" & oDataRow("EmployeeID")


So, when you loop through the controls on postback, You'll find one of your "special' check boxes, and you'll know what Employee was checked.

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-02-02 : 14:21:18
Exactly! Put in something in the ID to help you find the actual data item the checkbox represents.

The key, though, is to put them all into 1 single parent control to help you with both presentation and with later on retrieving the controls and their checked status. Panels are great things!

- Jeff
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-02-02 : 14:33:14
Jeff / Mike

Beautiful will try this out tomorrow, Im at home sick today..but working due to some problems with production db.

If only I was a chic what I'd do to you guys hahahaha .

One last question...would this also be sufficient for the code behind as well...meaning the triggering of an event..if I wanted to fire off the check box event of one of these dynamically created check boxes how would I do so?

Thanks,
Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-02-02 : 14:36:54
quote:
Originally posted by ehorn

It is a very nice example :)

Jon, is this something you will be doing in a datagrid or other type of repeating control?



Nah...kind of like the panel jeff was talking about...left side of my page that is why I originally said container. I usually intertwine panel and container to mean the same thing :). It wont be repeating, but it will definately change (the number of check boxes).

I see what jeff is doing and its slick, havent tried it yet, but conceptually. However, he did mention a button event that needs to be fired to check if the check box was checked. The problem with that Iis I need ot be able to trigger the event on the click of the actual check box.

But this is definately close!

Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-02-02 : 14:41:30
I'm not sure what you mean by "Fire off the check box event" ... do you mean add a handler to the CheckChanged event? I don't know if you can handle that event with server-side code.

If you mean can you dynamically check it on or off, sure -- just something like this:

DirectCast(panel1.FindControl( < control's ID here>), CheckBox).Checked = True

if it was an event you could handle server-side, on creation of the control you'd use AddHandler like this:

AddHandler chk.CheckedChanged, AddressOf CheckBox1_CheckedChanged

assuming "CheckBox1_CheckedChanged" was the name of a function with the proper signature. However, as mentioned, I don't believe server-side code can handle this event (I may be wrong).

- Jeff
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-02-02 : 14:44:48
quote:
Originally posted by jsmith8858

I'm not sure what you mean by "Fire off the check box event" ... do you mean add a handler to the CheckChanged event? I don't know if you can handle that event with server-side code.

If you mean can you dynamically check it on or off, sure -- just something like this:

DirectCast(panel1.FindControl( < control's ID here>), CheckBox).Checked = True

if it was an event you could handle server-side, on creation of the control you'd use AddHandler like this:

AddHandler chk.CheckedChanged, AddressOf CheckBox1_CheckedChanged

assuming "CheckBox1_CheckedChanged" was the name of a function with the proper signature. However, as mentioned, I don't believe server-side code can handle this event (I may be wrong).

- Jeff



Yes I guess this is what I mean...well sort of...
I'm not talking of actually assiging it dynamically (the value).

What I mean is lets say you created Check1 dynamically like your code.
Now I want the event "CheckChanged" associated with Check1. I guess your second response handles thaT? If I drag and drop a check box control in vs.net right now..and then I double click on it..it associates the event CheckChanged for Check1. How do I do this using the code though for creating a checkbox at runtime?




Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-02-02 : 14:51:10
EDIT: Just realized AutoPostBack is the thing to use to ensure the CheckedChange event is sent back to the server

If you want a server-side event to fire, you can do something like this when creating the CheckBox:

chk.AutoPostBack = True

That will submit the main form on the page as soon as a box is checked, and aparently it WILL call the server-side code you attached to the checkbox using the AddHandler technique shown above.

So, the full code of the page looks like this now:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Dim i As Integer
Dim chk As Web.UI.WebControls.CheckBox

For i = 1 To 10
With Panel1
chk = New CheckBox
chk.Text = "Value #" & i
chk.ID = "Check" & i
chk.Checked = False
chk.AutoPostBack = True
AddHandler chk.CheckedChanged, AddressOf CheckBox1_CheckedChanged

.Controls.Add(chk)
.Controls.Add(New LiteralControl("<BR>"))
End With
Next

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim c As Control
Response.Write("Button Pressed!<BR>")
For Each c In Panel1.Controls
If Left(c.ID, 5) = "Check" Then
If DirectCast(c, CheckBox).Checked Then
Response.Write(c.ID & " is checked!<BR>")
End If
End If
Next

End Sub


Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
Response.Write("Event! " & DirectCast(sender, WebControl).ID & " has been checked!<BR>")
End Sub



EDIT:
So, the key is to use AutoPostBack to ensure the server-side event is fired, and to use AddHandler to attach an event handler with newly created controls on your form...

- Jeff
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-02-02 : 14:54:42
So that will associate a check changed event for each check box or is that just a central on click for all of the check boxes and within that I just need to determine which check box was checked ?

Does what I just said even make sense :)


Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-02-02 : 14:55:44
yes -- the Sender proprety of the event tells you which control caused the event. Look at the code.

- Jeff
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-02-02 : 15:04:40
Just edited my last post -- the key is to use:

chk.AutoPostBack = True

to ensure the server receives the CheckedChange event.

- Jeff
Go to Top of Page

jhermiz

3564 Posts

Posted - 2005-02-02 : 15:07:22
quote:
Originally posted by jsmith8858

Just edited my last post -- the key is to use:

chk.AutoPostBack = True

to ensure the server receives the CheckedChange event.

- Jeff



That makes sense :).

I will give this a whirl sometime tomorrow or the following day.
Shanks for taking time out of your schedule to help guys.

Jon



Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]
Imperfection living for perfection --
[url]http://jhermiz.blogspot.com/[/url]
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2005-02-02 : 18:48:34
Just curious why you wouldn't use one of the built in repeating controls (Repeater, DataList, DataGrid)?

-Chad

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2005-02-02 : 19:23:18
Well, in my case I have a survey type app that has dynamic questions (and question types) with dynamic answers. I might have been able to get it to work with a repeater, but it seemed a bit easier to just dynamically add controls.

Getting the controls on the page is the easy part. Getting the data from said dynamically added controls is tough.

Michael


<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page
    Next Page

- Advertisement -