473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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/sys...ol.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(NameValueCollec tion 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/forum...ceholder.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.RaisePostDataCh angedEvent
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

Jun 28 '06 #1
0 2120

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dennis Johansson | last post by:
I have a CheckBoxList like this one: <asp:CheckBoxList id="chkReport" runat="server" DataValueField="rptID" DataTextField="RptName" Enabled="False" RepeatLayout="Flow"></asp:CheckBoxList> I...
2
by: Jay Walker | last post by:
I created a custom DataGridColumn based on Marcie Robillard's MSDN Article: Creating Custom Columns for the ASP.NET Datagrid...
4
by: dm_dal | last post by:
Is there a know issue surrounding the CheckBoxList control and it's viewstate? When my control is created, it's ListItems are checked as needed, but on a postback, they loose their Selected...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
2
by: Pipo | last post by:
Nobody knows how to get the values provided in the client can be read in the user-control? If have made a Web Custom Control with 2 textboxes and 1 dropdownlist. The user fills in my control (the...
2
by: Bart Van Hemelen | last post by:
The situation: I have a CheckBoxList cblTest, the items are disabled in cblTest_DataBound in a foreach (ListItem oItem in cblTest.Items) loop. I provide a link that calls a client-side JavaScript...
0
by: Walter | last post by:
Hi, can someone please help me with my custom control viewstate problem....I haven't slept for hours trying to get this fixed. I am making two custom controls which will be included on a single...
3
by: | last post by:
Hi all, I have a CheckBoxList control which has about 10 items. I have set autopostback=true and also set an eventhandler for OnSelectedIndexChanged. The problem is I want to identify which...
0
by: =?Utf-8?B?S04=?= | last post by:
I am getting the following exception in a page while using the Checkboxlist control. This happens on postback. startIndex cannot be larger than length of string. Parameter name: startIndex...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.