disable items in CheckBoxList

S

sql

Hi all,
I am using a CheckBoxList control on my aspx page that I bind to an array
at runtime. After I databind it, I check two of the items in the
CheckBoxList controls. Now I also need to disable these two items so that
users can't uncheck them. But I don't know how to disable individual items.
Can you please help me achieving this(preferably at runtime)?

Thanks in advance.
 
J

Jody Gelowitz

I am sure that this can be improved upon, but here is something that works.
To test the code out (listed below), simply add a CheckBoxList control to a
web form (name = "clb") and add the code below to the Page_Load event. When
you run the form, there will be 4 items which start out as unchecked. When
you click on an item, it will become checked and you will not be able to
uncheck it. This should give you a base to work from.

HTH,
Jody

__________________

Dim x() As String = {1, 2, 3, 4}

Dim intCount As Integer

Dim strItemName As String

Dim strScript As String

clb.DataSource = x

clb.DataBind()

For intCount = 0 To x.Length - 1

strItemName = clb.ClientID & "_" & intCount.ToString

strScript = "<script language = jscript for =" & strItemName & " event
=""onclick"">" & ControlChars.CrLf

strScript &= strItemName & ".checked='true';" & ControlChars.CrLf

strScript &= " </script> " & ControlChars.CrLf

RegisterClientScriptBlock("script" & intCount.ToString, strScript)

Next

___________________
 
S

sql

Thanks.

Jody Gelowitz said:
I am sure that this can be improved upon, but here is something that works.
To test the code out (listed below), simply add a CheckBoxList control to a
web form (name = "clb") and add the code below to the Page_Load event.
When you run the form, there will be 4 items which start out as unchecked.
When you click on an item, it will become checked and you will not be able
to uncheck it. This should give you a base to work from.

HTH,
Jody

__________________

Dim x() As String = {1, 2, 3, 4}

Dim intCount As Integer

Dim strItemName As String

Dim strScript As String

clb.DataSource = x

clb.DataBind()

For intCount = 0 To x.Length - 1

strItemName = clb.ClientID & "_" & intCount.ToString

strScript = "<script language = jscript for =" & strItemName & " event
=""onclick"">" & ControlChars.CrLf

strScript &= strItemName & ".checked='true';" & ControlChars.CrLf

strScript &= " </script> " & ControlChars.CrLf

RegisterClientScriptBlock("script" & intCount.ToString, strScript)

Next

___________________
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top