Custom CheckBoxList and Postback/ViewState exception (HtmlInputControl et al)

W

webmaster

Hi all,

I'm tearing my hair out with this one.

I have successfully implemented by own RadioButtonList in order to
provide additional functionality and a DIV rather than TABLE-based
layout in one of my ASP.NET 1.1 web forms. This involves a fairly
simple inheritance of the System.Web.UI.WebControls.RadioButtonList
class, with some new properties added and the Render sub overridden.
When I come to render each radio item, I do the following whilst
looping through all of the base ListItems:

Dim loInput As New HtmlControls.HtmlInputRadioButton
loInput.ID = Me.ID & "_" & i
loInput.Name = Me.ID
loInput.Value = MyBase.Items(i).Value

along with the following ASP.NET markup, after registering the FT
control assembly:

<FT:RadioList ID="RadioList" Runat="server">
<asp:ListItem Value="value1">Item 1</asp:ListItem>
<asp:ListItem Value="value2">Item 2</asp:ListItem>
<asp:ListItem Value="value3">Item 3</asp:ListItem>
...
</FT:RadioList>

All fairly straightforward, and renders thus:

<input value="value1" name="ControlName" id="ControlName_0"
type="radio"/>
<input value="value2" name="ControlName" id="ControlName_1"
type="radio"/>
<input value="value3" name="ControlName" id="ControlName_2"
type="radio"/>
...

A new requirement then meant that I needed to implement a
near-identically laid out list of CheckBoxes rather than RadioButtons.
I tried the following code:

Dim loInput As New HtmlControls.HtmlInputCheckBox
loInput.ID = Me.ID & "_" & i
loInput.Name = Me.ID
loInput.Value = MyBase.Items(i).Value

with the modified markup:

<FT:CheckBoxList ID="CheckBoxList" Runat="server">
<asp:ListItem Value="value1">Item 1</asp:ListItem>
<asp:ListItem Value="value2">Item 2</asp:ListItem>
<asp:ListItem Value="value3">Item 3</asp:ListItem>
...
</FT:CheckBoxList>

I was very happy that this small change didn't cause any other
compilation issues, but soon noticed a problem when the controls were
rendered:

<input value="value1" name="ControlName_0" id="ControlName_0"
type="checkbox"/>
<input value="value2" name="ControlName_1" id="ControlName_1"
type="checkbox"/>
<input value="value3" name="ControlName_2" id="ControlName_2"
type="checkbox"/>
...

The name attribute was equal to the id attribute instead of the value I
had specifically asked for. This meant that the Web Form was not
picking the correctly selected items from the control when requested.

I eventually found the reason for this in the MSDN (2, but also 1.1)
Library
(http://msdn2.microsoft.com/en-us/system.web.ui.htmlcontrols.htmlinputcontrol.name.aspx):

"HtmlInputControl.Name Property
~~~
Gets or sets the unique identifier name for the HtmlInputControl
~~~
In this implementation, the get accessor returns the value of the
Control.UniqueID property. However, the set accessor does not assign a
value to this property.
~~~
The set accessor does not assign a value to this property because the
Name property must have the same value as the Control.UniqueID property
for most controls to work properly."

After that blatant contradiction it then goes on to recommend that:

"Classes that inherit from the HtmlInputControl class may override
this implementation, if necessary."

Great, I thought. Time to get down and dirty with deriving my own
subclasses to unknown base classes. Actually, it wasn't that difficult
to put together what I thought was a working implementation, and with
just a few lines across two new classes I got things to render
correctly:

Public Class FTInputControl
Inherits System.Web.UI.HtmlControls.HtmlInputControl
Implements IPostBackDataHandler

Private prName As String = ""

Public Sub New(ByVal loType As String)
MyBase.New(loType)
End Sub

Public Overrides Property Name() As String
Get
Return prName
End Get
Set(ByVal Value As String)
prName = Value
End Set
End Property
End Class

Public Class FTInputCheckBox
Inherits FTInputControl
Public Sub New()
MyBase.New("checkbox")
End Sub
End Class

And in the custom CheckBoxList:

Dim loInput As New FTInputCheckBox
loInput.ID = Me.ID & "_" & i
loInput.Name = Me.ID
loInput.Value = MyBase.Items(i).Value

Excellent, I thought. But then came a Postback event and everything
went Pete Tong:

System.Web.HttpUnhandledException: Exception of type
System.Web.HttpUnhandledException was thrown. --->
System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length at System.String.Substring(Int32 startIndex,
Int32 length) at
System.Web.UI.WebControls.CheckBoxList.System.Web.UI.IPostBackDataHandler.LoadPostData(String
postDataKey, NameValueCollection postCollection) at
System.Web.UI.Page.ProcessPostData(NameValueCollection postData,
Boolean fBeforeLoad) at System.Web.UI.Page.ProcessRequestMain() ...

Now I have done a bit of research on this issue, and it seems like it
is something to do with the UniqueId property, ViewState and dealing
with Postback data (http://www.takempis.com/aspnet_anatomy2.asp,
http://www.mcse.ms/archive110-2004-9-918099.html,
http://www.velocityreviews.com/foru...checkboxlist-radiobuttonlist-placeholder.html).
Unfortunately, every implementation I have tried fails to fix the
exception. I can't even step into the code as it is at API level...

e.g. (courtesy of http://www.takempis.com/aspnet_anatomy2.asp):

Public Class FTInputControl
Inherits System.Web.UI.HtmlControls.HtmlInputControl
Implements IPostBackDataHandler

Private prName As String = ""

Public Event ServerChange As EventHandler

Public Sub New(ByVal loType As String)
MyBase.New(loType)
End Sub

Public Overrides Property Name() As String
Get
Return prName
End Get
Set(ByVal Value As String)
prName = Value
End Set
End Property

Public Function LoadPostData(ByVal postDataKey As String, ByVal
postCollection As System.Collections.Specialized.NameValueCollection)
As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData
If Value <> postCollection(postDataKey) Then
Value = postCollection(postDataKey)
Return True
End If
Return False
End Function

Public Sub RaisePostDataChangedEvent() Implements
System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent
RaiseEvent ServerChange(Me, EventArgs.Empty)
End Sub
End Class

I do not wish to use a set of asp:CheckBox-es that can be manually
checked (ahem!) for their 'checked' status as I will shortly be
databinding values to the list control rather than specifying them
manually as shown in the markup above.

Has anyone found a working fix or could recommend where I could try
next on this issue?

Thanks in advance,

Marc
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top