mostlylucid

scott galloway's personal blog...
posts - 916, comments - 758, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

Wierd Server Control issue - ever lose Viewstate for your Container object?

OK, bit of an unusual issue – but this happened to me when using a Server Control which extended another, parent control. Essentially I was binding an ITemplate on to a Container control to allow for DataBinding problem was that on reload, the child controls of my Server Control (in this instance, the Container) was not being recreated – which was a problem for saving content…Well the issue came down to not implementing INamingContainer on the child server control (I had it on the parent) – which results in problems for the control tracking it’s ViewState problem. Anyway, as I said odd issue but might save someone else the same problem! 

Print | posted on Tuesday, April 12, 2005 10:51 AM | Filed Under [ ASP.NET ]

Feedback

Gravatar

# re: Wierd Server Control issue - ever lose Viewstate for your Container object?

i got a problem, i write an control , the control allow user to use Tempalte mode to add any control to it(my designed-control), but i find a critical problem, any control that added in my control will lose it viewstate...it means that it can't recovery the pre-values. for example, if i add a button and a textbox controls to test Control. well, when i input some words to textbox, and then i click the button.......the textbox value will be clear.....??? how to keep control's value???

my test code is as following : ( I hope any one can help me, please.....thx)

<<test.cs>>

using System;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics.Design;
using System.Diagnostics;
using System.Drawing;

namespace WebAppControl{
/// <summary>
/// MainProcess
/// </summary>
public class test : System.Web.UI.WebControls.WebControl,INamingContainer,BasicTemplateContainerInterface{
private ITemplate _customDefinedContainer;

private ArrayList _menuItems = new ArrayList();
[Browsable(false)]
public ArrayList MenuItems{
get{return _menuItems;}
set{_menuItems = value;}
}

[Browsable(false)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateContainer(typeof(WebPanelContainer))]
public ITemplate CustomDefinedContainer{
get{return _customDefinedContainer;}
set{_customDefinedContainer = value;}
}

protected override void CreateChildControls() {
base.CreateChildControls ();
WebControl c = new WebControl(HtmlTextWriterTag.Span);
_customDefinedContainer.InstantiateIn(c);
TrackViewState();
this.Controls.Add(c);
}

protected override void OnPreRender(EventArgs e) {
base.OnPreRender (e);
CreateChildControls();
}
}
}


<<ControlTemplateControlDesigner.cs>>

using System;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Diagnostics.Design;
using System.Diagnostics;
using System.Reflection;

namespace WebAppControl{

public class ContentTemplateControlDesigner:TemplatedControlDesigner{
TemplateEditingVerb[] returnVerbs;


public ContentTemplateControlDesigner(){
}


protected override TemplateEditingVerb[] GetCachedTemplateEditingVerbs() {
if(returnVerbs == null) {
if(this.Component is BasicTemplateContainerInterface){
BasicTemplateContainerInterface btc = (BasicTemplateContainerInterface)this.Component;

returnVerbs = new TemplateEditingVerb[btc.MenuItems.Count+1];
returnVerbs[0] = new TemplateEditingVerb("Edit All",0,this);
for(int i=1;i<returnVerbs.Length;i++){
returnVerbs[i] = new TemplateEditingVerb(btc.MenuItems[i-1].ToString() , i ,this);
}
}else{
// unKnow Template
}
}
return returnVerbs;
}

protected override ITemplateEditingFrame CreateTemplateEditingFrame(TemplateEditingVerb verb) {
string newFrameName = "My Frame";
ITemplateEditingFrame newFrame = null;

ITemplateEditingService frameService = null;

string[] templateNames= new string[returnVerbs.Length-1];
for(int i=0;i<templateNames.Length;i++){
templateNames[i] = returnVerbs[i+1].Text;
}

if(returnVerbs != null && ((IList)returnVerbs).Contains(verb)){
frameService = (ITemplateEditingService)this.GetService(typeof(ITemplateEditingService));

if(frameService != null && verb.Index == 0){
newFrame = frameService.CreateFrame(this,newFrameName,templateNames,null,null);
}else{
for(int i=0;i<templateNames.Length;i++){
if(frameService != null && verb.Index == (i+1) ){
newFrame = frameService.CreateFrame(this,newFrameName,new string[]{templateNames[i]},null,null);
}
}
}
}
return newFrame;
}


public override string GetTemplateContent(ITemplateEditingFrame editingFrame, string templateName, out bool allowEditing) {
allowEditing = true;
string content = string.Empty;
if(this.Component is BasicTemplateContainerInterface){
BasicTemplateContainerInterface btc = (BasicTemplateContainerInterface)this.Component;
for(int i=0;i<btc.MenuItems.Count;i++){
if(templateName == btc.MenuItems[i].ToString() ){
if(btc.CustomDefinedContainer != null){
content = this.GetTextFromTemplate(btc.CustomDefinedContainer);
}
}
}
}else{
// unKnown Template
}

return content;
}

public override void SetTemplateContent(ITemplateEditingFrame editingFrame, string templateName, string templateContent) {
if(returnVerbs == null || ! ((IList)returnVerbs).Contains(editingFrame.Verb))
return;
if(this.Component is BasicTemplateContainerInterface){
BasicTemplateContainerInterface btc = (BasicTemplateContainerInterface)this.Component;
if(this.GetTemplateFromText(templateContent) != null){
for(int i=0;i<btc.MenuItems.Count;i++){
if(templateName == btc.MenuItems[i].ToString()){
btc.CustomDefinedContainer = this.GetTemplateFromText(templateContent);
}
}
}// end if
}
}
}
}
<<BasicTemplateContainerInterface.cs>>

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;

namespace WebAppControl
{
/// <summary>
///
/// </summary>
public interface BasicTemplateContainerInterface {
ArrayList MenuItems{
get;
set;
}
ITemplate CustomDefinedContainer{
get;
set;
}
}

}
5/6/2005 9:06 AM | help......
Comments have been closed on this topic.

Powered by: