CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums

ASP.NET Web Hosting – 3 Months Free!



Zone: > NEWSGROUP > Asp.Net Forum > windows_hosting.hosting_open_forum Tags:
Item Type: NewsGroup Date Entered: 9/16/2005 5:54:18 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 8 Views: 30 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
9 Items, 1 Pages 1 |< << Go >> >|
Afro Blanca
Asp.Net User
LoadViewState refuses to fire in a custom web control9/16/2005 5:54:18 PM

0/0

Here's the setup -

I have a custom webcontrol that is derived directly from System.Web.UI.WebControls.WebControl.  In PreRender(), I check a local variable for some data.  If the variable is empty, I call a function to generate some data.  I then store the data in the local variable.  The data is later persisted to the ViewState via SaveViewState().  I overrode LoadViewState() in such a way that, after every postback, the function should reload the data from the ViewState and store it in the local variable. 

Here's the problem -

LoadViewState never fires, ever, no matter how many times the page is posted back!  As a result, my local variable remains empty, and I must regenerate the data every time.  Why is this happening?  SaveViewState, on the other hand, goes off without a hitch.

Some details -

This control does not have any input elements.  All it does is display data from the overriden Render() method.

joteke
Asp.Net User
Re: LoadViewState refuses to fire in a custom web control9/17/2005 7:05:33 AM

0/0

Show the code for the control, and how it is added & placed to the Page.
Thanks,

Teemu Keiski
Finland, EU
cakewalkr7
Asp.Net User
Re: LoadViewState refuses to fire in a custom web control9/23/2005 1:06:29 PM

0/0

Did you get this figured out because I'm running into the same problem.  I just have...

protected override void LoadViewState(object savedState)

{

base.LoadViewState (savedState);

}

with a breakpoint on the base.LoadViewState line, but it never reaches it.

joteke
Asp.Net User
Re: LoadViewState refuses to fire in a custom web control9/23/2005 1:27:33 PM

0/0

 cakewalkr7 wrote:
Did you get this figured out because I'm running into the same problem.  I just have...

protected override void LoadViewState(object savedState)

{

base.LoadViewState (savedState);

}

with a breakpoint on the base.LoadViewState line, but it never reaches it.



Can you show how the control is used...
Thanks,

Teemu Keiski
Finland, EU
cakewalkr7
Asp.Net User
Re: LoadViewState refuses to fire in a custom web control9/23/2005 1:35:21 PM

0/0

Sure, first is the code for the control.  2nd is the containing page code.

namespace WebPEQ.Controls

{

/// <summary>

/// Summary description for RangeGroup.

/// </summary>

[DefaultProperty("ID"),

ToolboxData("<{0}:RangeGroup runat=server></{0}:RangeGroup>")]

public class RangeGroup : System.Web.UI.WebControls.WebControl, IAnswer, INamingContainer

{

private string[] _answers;

private HorizontalAlign _horAlign = HorizontalAlign.Left;

private VerticalAlign _vertAlign = VerticalAlign.Top;

private Alignment _placement = Alignment.Left;

private System.Web.UI.WebControls.Table OutputTable;

private bool _required = false;

private string _errMessage = string.Empty;

private System.Collections.ArrayList AnswerRadios;

#region Constructor

public RangeGroup(string strAnswers)

{

OutputTable = new Table();

}

#endregion

#region Ranges

//this is the only thing that I need to save to viewstate

public string[] Answers

{

get{

return _answers;

}

set

{

if (value != null && value.Length > 0)

{

_answers = new string[value.Length];

value.CopyTo(_answers, 0);

AnswerRadios = new System.Collections.ArrayList();

for (int i = 0; i < _answers.Length; i++)

AnswerRadios.Add(new RadioButton());

}

}

}

#endregion

#region Properties

public string Css

{

get

{

// if (ViewState["CSS"] == null)

return OutputTable.CssClass;

// else

// return ViewState["CSS"];

}

set{

OutputTable.CssClass = value.Trim();

// ViewState["CSS"] == value;

}

}

public bool Required

{

set{

// ViewState["Required"] = value;

_required = value;

}

get{

// if (ViewState["Required"] == null)

return _required;

// else

// return ViewState["Required"];

}

}

 

public string ErrorMessage

{

set{

// ViewState["ErrorMessage"] = value;

_errMessage = value.Trim();

}

get{

// if (ViewState["ErrorMessage"] != null)

// return ViewState["ErrorMessage"];

// else

return _errMessage;

}

}

 

#region Alignment Properties

public VerticalAlign VAlign

{

set

{

_vertAlign = value;

}

}

public HorizontalAlign TextAlign

{

set

{

_horAlign = value;

}

}

public Alignment TextPlacement

{

set

{

_placement = value;

}

}

#endregion

#endregion

#region Render

/// <summary>

/// Render this control to the output parameter specified.

/// </summary>

/// <param name="output"> The HTML writer to write out to </param>

protected override void Render(HtmlTextWriter output)

{

switch (_placement)

{

case Alignment.Bottom:

BuildVerticalTable(false);

break;

case Alignment.Left:

BuildHorizontalTable(true);

break;

case Alignment.Right:

BuildHorizontalTable(false);

break;

default:

BuildVerticalTable(true);

break;

}

this.Controls.Add(OutputTable);

OutputTable.RenderControl(output);

}

#endregion

#region BuildHorizontalTable

private void BuildHorizontalTable(bool TextOnLeft)

{

for (int i = 0; i < this._answers.Length; i++)

{

TableRow row = new TableRow();

TableCell c1 = new TableCell();

TableCell c2 = new TableCell();

if (TextOnLeft)

{

c1.Text = this._answersIdea [I].ToString().Trim();

RadioButton r = (RadioButton)AnswerRadiosIdea [I];

r.GroupName = this.ID;

r.EnableViewState = true;

//r.Checked = i==0?true:false;

c2.Controls.Add(r);

}

else

{

RadioButton radio = new RadioButton();

RadioButton r = (RadioButton)AnswerRadiosIdea [I];

r.GroupName = this.ID;

r.EnableViewState = true;

//r.Checked = i==0?true:false;

c1.Controls.Add(r);

c2.Text = this._answersIdea [I].ToString().Trim();

}

c1.VerticalAlign = _vertAlign;

c1.HorizontalAlign = this._horAlign;

c2.VerticalAlign = _vertAlign;

c2.HorizontalAlign = this._horAlign;

row.Cells.Add(c1);

row.Cells.Add(c2);

OutputTable.Rows.Add(row);

}

}

#endregion

#region BuildVerticalTable

private void BuildVerticalTable(bool TextOnTop)

{

TableRow TextRow = new TableRow();

TableRow RadioRow = new TableRow();

for (int i = 0; i < this._answers.Length; i++)

{

TableCell c = new TableCell();

c.VerticalAlign = this._vertAlign;

c.HorizontalAlign = this._horAlign;

c.Text = _answersIdea [I].ToString().Trim();

TextRow.Cells.Add(c);

c.Dispose(); c = null;

c = new TableCell();

c.VerticalAlign = this._vertAlign;

c.HorizontalAlign = this._horAlign;

RadioButton r = (RadioButton)AnswerRadiosIdea [I];

r.EnableViewState = true;

r.GroupName = this.ID;

//r.Checked = i==0?true:false;

c.Controls.Add(r);

RadioRow.Cells.Add(c);

c.Dispose(); c = null;

}

if (TextOnTop)

{

OutputTable.Rows.Add(TextRow);

OutputTable.Rows.Add(RadioRow);

}

else

{

OutputTable.Rows.Add(RadioRow);

OutputTable.Rows.Add(TextRow);

}

}

#endregion

#region Validate

public bool Validate()

{

if (_required)

{

int SelectedCount = 0;

// int RowCount = OutputTable.Rows.Count;

//

// for (int i = 0; i < RowCount; i++)

// {

// foreach (TableCell c in OutputTable.RowsIdea [I].Cells)

// {

// foreach (Control ctrl in c.Controls)

// {

// if (ctrl.GetType().ToString() == "System.Web.UI.WebControls.RadioButton")

// {

// RadioButton rd = (RadioButton)ctrl;

// if (rd.Checked)

// {

// SelectedCount = 1;

// break;

// }

// }

// if (SelectedCount > 0)

// break;

// }

// if (SelectedCount > 0)

// break;

// }

// }

for (int i =0; i < AnswerRadios.Count; i++)

{

RadioButton r = AnswerRadiosIdea [I] as RadioButton;

if (r.Checked)

{

SelectedCount = 1;

break;

}

}

if (SelectedCount == 0)

{

TableRow row = new TableRow();

TableCell c = new TableCell();

if (_errMessage != string.Empty)

c.Text = "<font color=\"red\">" + _errMessage + "</font>";

else

c.Text = "<font color=\"red\">Required Field</font>";

c.ColumnSpan = OutputTable.Rows[0].Cells.Count;

row.Cells.Add(c);

OutputTable.Rows.AddAt(0,row);

return false;

}

else

return true;

}

else

return true;

}

#endregion

protected override void LoadViewState(object savedState)

{

base.LoadViewState (savedState);

}

 

}

}



///////////////////////////containing page code here

public class WebForm1 : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Button Button1;

protected System.Web.UI.WebControls.PlaceHolder plcContainer;

private void Page_Load(object sender, System.EventArgs e)

{

}

private void LoadControls()

{

RangeGroup r = new RangeGroup();

r.ID = "33";

string[] texts = {"text1","text2","text3"};

r.Answers = texts;

r.TextPlacement = Alignment.Right;

r.TextAlign = HorizontalAlign.Left;

r.VAlign = VerticalAlign.Top;

r.ErrorMessage = "This is a required field.";

r.Required = true;

plcContainer.Controls.Add(r);

r.Dispose(); r = null;

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

LoadControls();

}

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.Button1.Click += new System.EventHandler(this.Button1_Click);

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

private void Button1_Click(object sender, System.EventArgs e)

{

RangeGroup rg = (WebPEQ.Controls.RangeGroup)Page.FindControl("33");   //keeps coming back null here

}

}

Afro Blanca
Asp.Net User
Re: LoadViewState refuses to fire in a custom web control9/23/2005 1:37:56 PM

0/0

Yeah, this is what I was doing wrong -

The problem was actually in my SaveViewState override, and not my LoadViewState override.  Here was my original code :

protected override object SaveViewState()

   object objSaved = base.SaveViewState();

   this
.ViewState[this.ID+"_output"]=this.m_strOut;
   
this.ViewState[this.ID+"_error"]=this.m_strErr; 

   return objSaved;
}

My mistake was in following the "convention" of always chaining to the base method as the first line in the method.  However, in this case, this is something that you don't want to do.  My corrected code is as follows  :

protected override object SaveViewState()

   
this.ViewState[this.ID+"_output"]=this.m_strOut;
   this.ViewState[this.ID+"_error"]=this.m_strErr; 
   
   return
base.SaveViewState();
}


joteke
Asp.Net User
Re: LoadViewState refuses to fire in a custom web control9/23/2005 3:38:29 PM

0/0

You are adding OutputTable to child controls at Render stage.

protected override void Render(HtmlTextWriter output)

{

...

this.Controls.Add(OutputTable);

OutputTable.RenderControl(output);

}



At that point no viewstate is loaded anymore (Since page's entire viewstate is saved at that point). E.g it is page lifecycle issue. You'd need to add the table earlier (CreateChildControls or Load or Prerender or control instantiation if possible if that happens before Render)

Another thing is that you have overridden LoadViewState but I can't see override for SaveViewState (both overrides would need to exist).
Thanks,

Teemu Keiski
Finland, EU
cakewalkr7
Asp.Net User
Re: LoadViewState refuses to fire in a custom web control9/28/2005 5:56:17 PM

0/0

Okay, I rewrote part of the code and moved the this.Controls.Add(OutputTable) to my CreateChildControls method.  However, it's still not holding it's state.  Do I need to override the SaveViewState and the LoadViewState methods?  If so, I'm unclear on how I go about saving and loading the view state with my radio objects.  Can anyone offer some code help?  Thanks!

namespace WebPEQ.Controls
{
	/// 
	/// Summary description for RangeGroup.
	/// 
	[DefaultProperty("ID"), 
		ToolboxData("<{0}:RangeGroup runat=server>")]
	public class RangeGroup : System.Web.UI.WebControls.WebControl, IAnswer, INamingContainer
	{
		private string[] _answers;
		private HorizontalAlign _horAlign = HorizontalAlign.Left;
		private VerticalAlign _vertAlign = VerticalAlign.Top;
		private Alignment _placement = Alignment.Left;
		private System.Web.UI.WebControls.Table OutputTable;
		private bool _required = false;
		private string  _errMessage = string.Empty;
		private System.Collections.ArrayList AnswerRadios;

		#region Constructor
		public RangeGroup(string[] Answers)
		{
			OutputTable = new Table();
			_answers = Answers;
			AnswerRadios = new System.Collections.ArrayList(_answers.Length);
		}
		#endregion
		protected override void CreateChildControls()
		{
			this.Controls.Add(OutputTable);
			for (int i = 0; i < _answers.Length; i++)
				AnswerRadios.Add(new RadioButton());
			base.CreateChildControls ();
		}

		#region Properties
		public string Css
		{
			get
			{
					return OutputTable.CssClass;
			}
			set{
				OutputTable.CssClass = value.Trim();
			}
		}

		public bool Required
		{
			set{ 
				_required = value;
			}
			get{ 			
					return _required;
			}
		}


		public string ErrorMessage 
		{
			set{
				_errMessage = value.Trim();
			}
			get{ 
					return _errMessage;
			}
		}


		#region Alignment Properties
		public VerticalAlign VAlign
		{
			set 
			{
				_vertAlign = value;
			}
		}

		public HorizontalAlign TextAlign
		{
			set 
			{
				_horAlign = value;
			}
		}

		public Alignment TextPlacement
		{
			set
			{
				_placement = value;
			}
		}
		#endregion
		#endregion

		#region Render
		///  
		/// Render this control to the output parameter specified.
		/// 
		///  The HTML writer to write out to 
		protected override void Render(HtmlTextWriter output)
		{
			switch (_placement)
			{
				case Alignment.Bottom:
					BuildVerticalTable(false);
					break;
				case Alignment.Left:
					BuildHorizontalTable(true);
					break;
				case Alignment.Right:
					BuildHorizontalTable(false);
					break;
				default:
					BuildVerticalTable(true);
					break;
			}

			OutputTable.RenderControl(output);
		}
		#endregion

		#region BuildHorizontalTable
		private void BuildHorizontalTable(bool TextOnLeft)
		{
			

			for (int i = 0; i < this._answers.Length; i++)
			{
				TableRow row = new TableRow();
				TableCell c1 = new TableCell();
				TableCell c2 = new TableCell();

				if (TextOnLeft)
				{
					c1.Text = _answersIdea [I].ToString().Trim();
					RadioButton r = (RadioButton)AnswerRadiosIdea [I]; 
					c2.Controls.Add(r);
					r.GroupName = this.ID;
					r.EnableViewState = true;
				}
				else
				{
					RadioButton radio = new RadioButton();
					RadioButton r = (RadioButton)AnswerRadiosIdea [I];
					c1.Controls.Add(r);
					r.GroupName = this.ID;
					r.EnableViewState = true;
					c2.Text = _answersIdea [I].ToString().Trim();
				}

				c1.VerticalAlign = _vertAlign;
				c1.HorizontalAlign = this._horAlign;
				c2.VerticalAlign = _vertAlign;
				c2.HorizontalAlign = this._horAlign;
				row.Cells.Add(c1);
				row.Cells.Add(c2);

				OutputTable.Rows.Add(row);
			}
		}
		#endregion

		#region BuildVerticalTable
		private void BuildVerticalTable(bool TextOnTop)
		{
			TableRow TextRow = new TableRow();
			TableRow RadioRow = new TableRow();

			for (int i = 0; i < this._answers.Length; i++)
			{
				TableCell c = new TableCell();
				c.VerticalAlign = _vertAlign;
				c.HorizontalAlign = _horAlign;
				c.Text = _answersIdea [I].ToString().Trim();
				TextRow.Cells.Add(c);
				c.Dispose(); c = null;

				c = new TableCell();
				c.VerticalAlign = _vertAlign;
				c.HorizontalAlign = _horAlign;
				RadioButton r = (RadioButton)AnswerRadiosIdea [I];
				c.Controls.Add(r);
				r.EnableViewState = true;
				r.GroupName = this.ID;
				RadioRow.Cells.Add(c);
				c.Dispose(); c = null;
			}

			if (TextOnTop)									
			{
				OutputTable.Rows.Add(TextRow);
				OutputTable.Rows.Add(RadioRow);
			}
			else
			{
				OutputTable.Rows.Add(RadioRow);
				OutputTable.Rows.Add(TextRow);
			}
		}
		#endregion



	
	}
}
cakewalkr7
Asp.Net User
Re: LoadViewState refuses to fire in a custom web control9/28/2005 6:16:56 PM

0/0

Nevermind, I changed my code to
		protected override void CreateChildControls()
{
this.Controls.Add(OutputTable);
for (int i = 0; i < _answers.Length; i++)
AnswerRadios.Add(new RadioButton());
base.CreateChildControls ();
switch (_placement)
{
case Alignment.Bottom:
BuildVerticalTable(false);
break;
case Alignment.Left:
BuildHorizontalTable(true);
break;
case Alignment.Right:
BuildHorizontalTable(false);
break;
default:
BuildVerticalTable(true);
break;
}
}

...so that it added all the controls to the control in that method and now it's keeping state fine.
9 Items, 1 Pages 1 |< << Go >> >|


Free Download:


Web:
Building a Custom Registration and Login Control The default base class in the Web Custom Control template is System.Web.UI. .... the SaveViewState and LoadViewState methods of the Control base class: ...
Asp Net Interview Questions – Asp.Net 2005 Interview Questions,Asp ... Web custom controls:-Web Custom Control is typical to create and gud for .... Clients might refuse cookies, so your code has to anticipate that possibility. ...
CodeProject: MasterPages reinvented - a Component Based Template ... Failed to load viewstate. The control tree into which viewstate is being .... with the Web Form so I didn't need to create a custom interface to handle the ...
.NET ASP Page 47 - Bytes Site Map NET 2 Odd Error Message · CS0103 - strange errors · Atlas UpdatePanel in Web User Control - How to update other controls? ...
Infinities Loop: 03_06 Web.UI.Control (again, the class that every control, user control, and page derive from) is a LoadViewState() method which accepts an object as parameter. ...
ASP Net [Archive] - Page 286 Solution: LoadViewState not getting fired in ASP.NET 2.0 · Access Denied, ... No connection could be made because the target machine actively refused it ...
.NET ASP Page 52 - Bytes Site Map NET 2.0 then use for joined table in SQL Server 2005 Stored Procedure · Custom control - how to access text inside the tag · How to create clear text ...
Vishal Joshi's Tangent: 11/01/2004 - 12/01/2004 This also many times takes custom control writers by surprise as they are ... WebControl.LoadViewState System.Web.UI.Page.EnsureChildControls System.Web.UI. ...
ASP.NET Development Jun 10, 2006 ... Custom web controls .... CreateUserWizard fails to fire Deactivate event handler if web page is placed in a .... Failed to Load ViewState? ...
Mrunal Brahmbhatt One of the main attractions of Web Forms is the rich control model that has resulted in a huge proliferation of custom controls from third parties that ...




Search This Site:










control properties defined procedurally

hosting solution

access controls from class from within user control class

page and its variables

webformdesigner

using a validator on a custon web control

composite control event problem

hosting website

sbs 2003 hosting cal problem

custom control question.

hosted crm with reporting services 2005

user control for multiple applications

document properties in sql show like outlook.

ascx a crossed applications

event trapping between controls

how can i give my control design time support?

how to create editable datagrid on usercontrol page ?

applying styles to button

application-level registration of user controls?

web hosting

designer verbs for properties

add an icon (toolbar) to user controls

checkbox prerender event

this cannot, cannot be true...

treeview expanding

templated user control

server control that is used dozens of times on one page - question

question on control creation

help on treeview blowing up during post back

.net remoting communication

  Privacy | Contact Us
All Times Are GMT