Saturday, March 24, 2012

simple custom control question

I am makng my first custom control where I am extending the checkBoxList control so that you can get the number of checkboxes that are ticked. But, for some reason, I always get the number of items in the checkboxlist instead of the number that are ticked.
Can someone please tell me what I am doing wrong. Here is the code.

Imports System.ComponentModel
Imports System.Web.UI

<DefaultProperty("Text"), _
ToolboxData("<{0}:JCheckBox runat=server></{0}:JCheckBox>")> _
Public Class JCheckBox
Inherits System.Web.UI.WebControls.CheckBoxList

ReadOnly Property getCheckBoxTickedCount() As Integer
Get
Me.EnsureChildControls()
Dim i As Integer
Dim count As Integer = 0
For i = 0 To Me.Items.Count - 1
If Me.Items(i).Selected Then
count += 1
End If
Next
Return i
End Get
End Property

End Class

Return i
should be
Return count

Doh, thanks a lot.
Just wondering if you could help me with one more thing.

With the classic checkbox that comes with VS2002 you can do:
<asp:CheckBoxList ID="checkbox" Runat="server">
<asp:ListItem>This is just the asp verion</asp:ListItem>
<asp:ListItem>This is not my verion</asp:ListItem>
<asp:ListItem>This is just the asp verion</asp:ListItem>
<asp:ListItem>This is not my verion</asp:ListItem>
<asp:ListItem>This is just the asp verion</asp:ListItem>
<asp:ListItem>This is not my verion</asp:ListItem>
</asp:CheckBoxList
With my extended checkbox, I would like to do the following:
<asp:JCheckBoxList ID="checkbox" Runat="server">
<asp:ListItem itemID="myID1">This is just the asp verion</asp:ListItem>
<asp:ListItem itemID="myID2">This is not my verion</asp:ListItem>
<asp:ListItem itemID="myID3">This is just the asp verion</asp:ListItem>
<asp:ListItem itemID="myID4">This is not my verion</asp:ListItem>
<asp:ListItem itemID="myID5">This is just the asp verion</asp:ListItem>
<asp:ListItem itemID="myID6">This is not my verion</asp:ListItem>
</asp:JCheckBoxList
So, this means that I can then, in the code, do something like:

JCheckBox1.getItemByID("myID1").selected = true

instead of

CheckBox1.items(0).selected = true

Can you just guide me on how I can do this. A simple tutorial or a push in the right direction would be greatly appriciated.
Ok, doh. just though I could use the ListItem.Value instead.

But... I have made the control. When I put it on the page, VS2002 underlines all the asp:ListItems saying that the active schema doesnt support them. Can anyone tell me why, when the page actually works?

Also, is there any way of turning the intellisence on for custom controls.
The active schema generally does not have anything to do with what normally works, it serves more to correct.

For example, I have a basic HTML page (not .NET) I wrote in VS.NET for the syntax highlighting and it complains about the <style type="text/css" media="all"> tag.

>Also, is there any way of turning the intellisence on for custom controls.

Look into attributes. (Here's a nice one)

0 comments:

Post a Comment