473,695 Members | 2,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom CheckBoxList and Postback/ViewState exception (HtmlInputContr ol 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.W ebControls.Radi oButtonList
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.Ht mlInputRadioBut ton
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="ControlNa me" id="ControlName _0"
type="radio"/>
<input value="value2" name="ControlNa me" id="ControlName _1"
type="radio"/>
<input value="value3" name="ControlNa me" 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.Ht mlInputCheckBox
loInput.ID = Me.ID & "_" & i
loInput.Name = Me.ID
loInput.Value = MyBase.Items(i) .Value

with the modified markup:

<FT:CheckBoxLis t ID="CheckBoxLis t" 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="ControlNa me_0" id="ControlName _0"
type="checkbox"/>
<input value="value2" name="ControlNa me_1" id="ControlName _1"
type="checkbox"/>
<input value="value3" name="ControlNa me_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):

"HtmlInputContr ol.Name Property
~~~
Gets or sets the unique identifier name for the HtmlInputContro l
~~~
In this implementation, the get accessor returns the value of the
Control.UniqueI D 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.UniqueI D property
for most controls to work properly."

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

"Classes that inherit from the HtmlInputContro l 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.H tmlControls.Htm lInputControl
Implements IPostBackDataHa ndler

Private prName As String = ""

Public Sub New(ByVal loType As String)
MyBase.New(loTy pe)
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("che ckbox")
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.Http UnhandledExcept ion: Exception of type
System.Web.Http UnhandledExcept ion was thrown. --->
System.Argument OutOfRangeExcep tion: Length cannot be less than zero.
Parameter name: length at System.String.S ubstring(Int32 startIndex,
Int32 length) at
System.Web.UI.W ebControls.Chec kBoxList.System .Web.UI.IPostBa ckDataHandler.L oadPostData(Str ing
postDataKey, NameValueCollec tion postCollection) at
System.Web.UI.P age.ProcessPost Data(NameValueC ollection postData,
Boolean fBeforeLoad) at System.Web.UI.P age.ProcessRequ estMain() ...

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.H tmlControls.Htm lInputControl
Implements IPostBackDataHa ndler

Private prName As String = ""

Public Event ServerChange As EventHandler

Public Sub New(ByVal loType As String)
MyBase.New(loTy pe)
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(By Val postDataKey As String, ByVal
postCollection As System.Collecti ons.Specialized .NameValueColle ction)
As Boolean Implements System.Web.UI.I PostBackDataHan dler.LoadPostDa ta
If Value <> postCollection( postDataKey) Then
Value = postCollection( postDataKey)
Return True
End If
Return False
End Function

Public Sub RaisePostDataCh angedEvent() Implements
System.Web.UI.I PostBackDataHan dler.RaisePostD ataChangedEvent
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 2142

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

Similar topics

1
1713
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 fill it with items from a database and check/select items corresponding to another database query. When first displayed, the user cannot select/deselect items since Enabled=false. After pushing a button on the page the list gets
2
3191
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 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/creatingcustomcolumns.asp The problem I am having is that the data in the custom datagridcolumn is not saved to viewstate and after postback, the column does not contain data.
4
8068
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 status. David
7
2994
by: Shimon Sim | last post by:
I have a custom composite control I have following property
2
1692
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 textboxes and the dropdownlist) and lots more stuff on the page. When the user wants to save the page he'll click the save button. The server gets the postback but I can read out the filled in controls (in my control). The textboxes text = ""...
2
6248
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 that enables the items -- this works perfectly. However, when I then click a LinkButton that does a postback and I loop through the items on serverside to pick up the selected ones, it doesn't pick them up. IMHO it seems to loop though the items...
0
1660
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 page. For now, I will call them box1 and box2. Each box has a panel and will display a list of link buttons on the panel. When the page loads for the first time, box1 will call the database and create a list of linkbuttons and each link button has...
3
3485
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 checkbox item caused the event to fire to perform some routines based upon the value of that individual checkbox. How can I determine this? For example, 5 of the 10 items maybe checked. However, when I uncheck one, I want to perform some code based...
0
1048
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 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details:...
0
8647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9129
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9004
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7681
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3023
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1986
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.