Answered by:
Collection disappear

Question
-
User-702247205 posted
Hello Everyone,
I have the same problem as this poster:
http://forums.asp.net/t/1731492.aspx/1?Problems+rendering+controls+collection+at+design+time
I am able to fill value with the collection editor in the form editor but I does not show at run time. Do I forget to implement a function?
I will attach the code of 3 classes in the next posts.
Also, I post the code here: http://www.hollox.net/a/HlxControl.zip
Thank you,
Have a great day!
Sébastien
Tuesday, October 25, 2011 3:38 PM
Answers
-
User-1488931086 posted
Hi——
I'm afraid that you haven't kept the ArrayList into something like ViewState or Session, Because of this, everytime when you do a postBack and the control is re-created and all the data lost.
Plz check your CreateChildrenControls or something like this else.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 26, 2011 10:23 PM -
User3866881 posted
Hello:)
protected override void OnInit(EventArgs e)
{
arrSelectedValue = new
ArrayList();
arrAvailableValue = new ArrayList();
base.OnInit(e);
EnsureChildControls();
}In runtime the arrAvailableValue
will be assigned before this event handler and thus the assigned value is lost
by the code he added. soPlease remove the two bold statements and have a try:)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 27, 2011 4:28 AM
All replies
-
User-702247205 posted
File: DoubleListCheck.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections.Specialized;
using System.Web;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Drawing.Design;
namespace HlxControl
{
[ParseChildren(true)
,PersistChildren(false)]
public class DoubleListCheck : WebControl, IPostBackDataHandler
{
private ArrayList arrSelectedValue;
private ArrayList arrAvailableValue;
//------------------------------------------------------------------------------------
// C L I E N T I N S T A N C E N A M E
//
[Bindable(true)
,Category("Appearance")]
public string ClientInstanceName
{
get
{
string strValue = (string)ViewState["ClientInstanceName"];
string strResult;
if (strValue != null)
strResult = strValue;
else
strResult = string.Empty;
return strResult;
}
set
{
ViewState["ClientInstanceName"] = value;
}
}
//------------------------------------------------------------------------------------
// A V A I L A B L E V A L U E
//
[Bindable(true)
,Category("Data")
,Description("List of available values")
,Editor(typeof(DoubleListCheckEditor), typeof(UITypeEditor))
,PersistenceMode(PersistenceMode.InnerProperty)]
public ArrayList AvailableValue
{
get
{
if (arrAvailableValue == null)
arrAvailableValue = new ArrayList();
return arrAvailableValue;
}
}
//------------------------------------------------------------------------------------
// S E L E C T E D V A L U E
//
[Bindable(true)
,Category("Data")
,Description("List of selected values")
,Editor(typeof(DoubleListCheckEditor), typeof(UITypeEditor))
,PersistenceMode(PersistenceMode.InnerProperty)]
public ArrayList SelectedValue
{
get
{
if (arrSelectedValue == null)
arrSelectedValue = new ArrayList();
return arrSelectedValue;
}
}
//------------------------------------------------------------------------------------
// C R E A T E C H I L D C O N T R O L S
//
// Override the CreateChildControls method to
// add child controls to the Employees property when this
// custom control is requested from a page.
protected override void CreateChildControls()
{
base.CreateChildControls();
Controls.Clear();
CreateControlHierarchy();
ClearChildViewState();
}
//------------------------------------------------------------------------------------
// A D D A V A I L A B L E I T E M
//
public void AddAvailableItem(string strDescription, string strID, string strValue)
{
DoubleListCheckItem oItem = new DoubleListCheckItem(strID, strDescription, strValue);
arrAvailableValue.Add(oItem);
}
//------------------------------------------------------------------------------------
// A D D S E L E C T E D I T E M
//
public void AddSelectedItem(string strDescription, string strID, string strValue)
{
DoubleListCheckItem oItem = new DoubleListCheckItem(strID, strDescription, strValue);
arrSelectedValue.Add(oItem);
}
//------------------------------------------------------------------------------------
// A D D R O W
//
private string AddRow(string strDescription, string strID, string strValue)
{
string strHTML = string.Empty;
strHTML += "<tr class=\"jsDoubleListCheck_row\">";
strHTML += " <td><input type=\"checkbox\" /></td>";
strHTML += " <td>" + strDescription + "</td>";
strHTML += " <td><input type=\"text\" size=\"4\" maxlength=\"4\" value=\"" + strValue + "\"/></td>";
strHTML += " <td><input type=\"hidden\" value=\"" + strID + "\" /></td>";
strHTML += "</tr>";
return strHTML;
}
//------------------------------------------------------------------------------------
// R E N D E R
//
protected string renderHtml()
{
// INITIALIZE
if (ClientInstanceName == string.Empty)
ClientInstanceName = this.ID + "_js";
string m_ID = ClientInstanceName;
string strHTML = string.Empty;
// OPEN OBJECT
strHTML += "<div class=\"jsDoubleListCheck_frame\">";
// OPEN ROW DATA
strHTML += "<div class=\"jsDoubleListCheck_row\">";
// COLUMN SELECTED
strHTML += " <div class=\"jsDoubleListCheck_column\">";
strHTML += " <table id=\"" + m_ID + "_table_selected\" class=\"jsDoubleListCheck_table jsDoubleListCheck_column_selected\">";
foreach (DoubleListCheckItem oItem in SelectedValue)
strHTML += AddRow(oItem.Description, oItem.Value, "");
strHTML += " </table>";
strHTML += " </div>";
// COLUMN AVAILABLE
strHTML += " <div class=\"jsDoubleListCheck_column\">";
strHTML += " <table id=\"" + m_ID + "_table_available\" class=\"jsDoubleListCheck_table jsDoubleListCheck_column_available\">";
foreach (DoubleListCheckItem oItem in AvailableValue)
strHTML += AddRow(oItem.Description, oItem.Value, "");
strHTML += " </table>";
strHTML += " </div>";
// CLOSE ROW DATA
strHTML += "</div>";
// OPEN ROW BUTTON
strHTML += "<div class=\"jsDoubleListCheck_row\">";
// MOVE TO RIGHT
strHTML += " <div class=\"jsDoubleListCheck_column\">";
strHTML += " <input class=\"jsDoubleListCheck_button\" type=\"button\" value=\"Remove\" onclick=\"" + m_ID + ".moveRow('selected', 'available')\" />";
strHTML += " </div>";
// MOVE TO LEFT
strHTML += " <div class=\"jsDoubleListCheck_column\">";
strHTML += " <input class=\"jsDoubleListCheck_button\" type=\"button\" value=\"Add\" onclick=\"" + m_ID + ".moveRow('available', 'selected')\" />";
strHTML += " </div>";
// CLOSE ROW BUTTON
strHTML += "</div>";
// START - DATA
strHTML += "<div class=\"jsDoubleListCheck_row\">";
strHTML += "<input style=\"visibility: hidden;\" id=\"" + m_ID + "_data\" type=\"text\" />";
strHTML += "</div>";
// END - DATA
// CLOSE OBJECT
strHTML += "</div>";
// JAVASCRIPT
strHTML += "<script language=\"javascript\" type=\"text/javascript\">";
strHTML += "// COMPONENT INITIALIZATION";
strHTML += "\nvar " + m_ID + " = new jsDoubleListCheck('" + m_ID + "');";
strHTML += "</script>";
// RETURN
return strHTML;
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write(renderHtml());
/*
TextBox oTextbox1;
writer.AddAttribute("border", "1");
writer.AddAttribute("cellpadding", "0");
writer.AddAttribute("cellspacing", "0");
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
// Label
writer.RenderBeginTag(HtmlTextWriterTag.Td);
oLabel.RenderControl(writer);
writer.RenderEndTag(); //Td
// Textbox
writer.RenderBeginTag(HtmlTextWriterTag.Td);
oTextbox.RenderControl(writer);
writer.RenderEndTag(); //Td
writer.RenderEndTag(); //Tr
foreach (DoubleListCheckItem oItem in SelectedValue)
{
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
oTextbox1 = new TextBox();
oTextbox1.Text = oItem.Description;
oTextbox1.RenderControl(writer);
writer.RenderEndTag(); //Td
writer.RenderEndTag(); //Tr
}
writer.RenderEndTag(); //Table
//RenderContents(writer);*/
}
protected override void OnInit(EventArgs e)
{
arrSelectedValue = new ArrayList();
arrAvailableValue = new ArrayList();
base.OnInit(e);
EnsureChildControls();
}
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postValues)
{
string postedValue = string.Empty;
/* if (postValues[oTextbox.UniqueID] != null)
postedValue = postValues[oTextbox.UniqueID].ToString();
if (postedValue != Text)
{
Text = postedValue;
return true;
}*/
return false;
}
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
}
//---------------------------------------------------------------------------------------------------
// C R E A T E C O N T R O L H I E R A R C H Y
//
/// <summary>
///
/// </summary>
protected virtual void CreateControlHierarchy()
{
/* oTextbox = new TextBox();
oTextbox.Text = Text;
this.Controls.Add(oTextbox);
oLabel = new Label();
oLabel.Text = Title;
this.Controls.Add(oLabel);*/
// Register a helper hidden field
Page.ClientScript.RegisterHiddenField(ID, "");
}
protected override void AddParsedSubObject(object o)
{
int i;
i = 0;
}
//protected override Type
}
}
Tuesday, October 25, 2011 3:38 PM -
User-1488931086 posted
Hi——
I'm afraid that you haven't kept the ArrayList into something like ViewState or Session, Because of this, everytime when you do a postBack and the control is re-created and all the data lost.
Plz check your CreateChildrenControls or something like this else.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 26, 2011 10:23 PM -
User3866881 posted
Hello:)
protected override void OnInit(EventArgs e)
{
arrSelectedValue = new
ArrayList();
arrAvailableValue = new ArrayList();
base.OnInit(e);
EnsureChildControls();
}In runtime the arrAvailableValue
will be assigned before this event handler and thus the assigned value is lost
by the code he added. soPlease remove the two bold statements and have a try:)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 27, 2011 4:28 AM -
User-702247205 posted
Hello TimoYang and DeckerDong!
Thank you for your answers!
I implemented serialize on DoubleCheckItem (TimoYang)
I removed the code from the OnInit (DeckerDong).
Then I created a subcontrol so there is one collection that is managed per control as instructed on this blog:
www.west-wind.com/weblog/posts/2004/Jan/14/Collection-properties-in-ASPNet-Server-Controls-Building-a-TabStrip-Control
Thank you!
Sébastien
Thursday, October 27, 2011 7:40 AM