473,397 Members | 2,068 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,397 software developers and data experts.

Problem with Postback of CollectionBase

Hello friends

I am trying to study about CollectionBase. I have inherited CollectionBase in the class TheCollection.cs. I am using the “TheCollection. in an aspx page to store objects of the class ElementClass.cs.
When I click the Button2 an exception occurs….index out of range….This happens because of the fact that the class I added by clicking Button1 is lost when I click the Button2…This problem is associated with Postback of CollectionBase …
Can anyone suggest me workaround? Is it a bug? As I am in the course of studying CollectionBase, a solution which ask for replacing Collection base with some other functionality will not be a good solution

The files are given below
[code]

1) ElementClass.cs

public class ElementClass
{
public string Name;
public ElementClass()
{

}

}

2) TheCollection.cs

public class TheCollection : System.Collections.CollectionBase
{
public TheCollection()
{
}
public void Add(ElementClass aWidget)
{
List.Add(aWidget);
}
public void Remove(int index)
{

if (index > Count - 1 || index < 0)
{

}

else
{
List.RemoveAt(index);
}
}

public ElementClass Item(int Index)
{

return (ElementClass)List[Index];
}
}


3)SamplePage.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SamplePage.aspx.cs" Inherits="SamplePage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" Enabled="False" />
<asp:Label ID="Label2" runat="server" BackColor="#FFFF80" Text="Label" Width="232px"></asp:Label></div>
</form>
</body>
</html>


4)SamplePage.aspx.cs

public partial class SamplePage : System.Web.UI.Page
{
TheCollection objCollection = new TheCollection();

protected void Page_Load(object sender, EventArgs e)
{
////ElementClass second = new ElementClass();
////second.Name = "theSecond111";
////objCollection.Add(second);
}
protected void Button1_Click(object sender, EventArgs e)
{


ElementClass aWidget = new ElementClass();
aWidget.Name = TextBox1.Text;
objCollection.Add(aWidget);

Button2.Enabled = true;
Label1.Text = (objCollection.Count).ToString();


}
protected void Button2_Click(object sender, EventArgs e)
{
ElementClass another = new ElementClass();
another.Name = TextBox1.Text;

objCollection.Add(another);

Label1.Text = (objCollection.Count).ToString();
Label2.Text = (objCollection.Item(1)).Name;
}
}

[ /CODE]

Note:----This will work fine if the commented out section of SamplePage.aspx.cs is uncommented

Note_____ You can search in google with the topic “control collection life collectionbase postback” .But Nothing gives me required solution


With Regards
LIJO CHEERAN
Jul 27 '07 #1
0 1140

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Steve M | last post by:
I have subclassed CollectionBase. I have also implemented GetEnumerator(). I have tried to set the DataSource of a DataGrid to an instance of my subclass. However, the items in the grid are not...
1
by: alanrn | last post by:
I've implemented a number of strongly-typed collections that inherit from CollectionBase and recently noticed something that I don't fully understand. CollectionBase defines method RemoveAt(). ...
5
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
3
by: Jeremy | last post by:
I am creating a custom collection. Very simple. Imports System.Collections Public Class ShapesCollection Inherits BaseCollection Default Public Property Item(ByVal index As Integer) As...
2
by: Samuel R. Neff | last post by:
What's the advantage of inheriting from CollectionBase as opposed to just implementing IList? It seems that it saves you from having to implement a few properties (Clear, CopyTo, Count,...
2
by: Smokey Grindle | last post by:
I have the following on my page... a link button that will send the command name "use" to the command handler and a grid view with the following columns 0 - Template column - just a checkbox...
6
by: Tony Johansson | last post by:
Hello! Assume I have this inheritance see below. I know that a much better alternative is to use generics but I just want to know how things works. According to the documentation in...
3
by: Tony Johansson | last post by:
Hello! Sorry for opening up this task again. I want to fully understand this List that is return from CollectionBase. According to you is List in CollectionBase implemented something like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.