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