ViewState is late when building own Controls with different subcontrols

R

Rolf Welskes

Hello,
the problem seems simple

I have an own webcontrol

one property is

public bool IsEdit
{
get
{
...
}
set
{
...
}


//then there is CreateChildControls()

override void CreateChildControls()
{
if(IsEdit)
{
TextBox tx = new TextBox()
Controls.Add(tx)
}
else
{
Label lbl = new Label()
Controls.Add(lbl);
}
}

this is the princip. If the property IsEdit is true, an edit-control should
be shown, if not a label should be shown.
Now if for example IsEdit is true und saved in the ViewState.
Now a Postback is made.
So, the CreateChildControl should create the editbox, but no it does not,
because the ViewState is available after the call of CreateChildControls.
It also is not possible to call CreateChildControls later from my own code,
because the textbox has text also saved over the viewState and
after postback the textbox must be availabel to set the text of the
viewstate (bei the asp-system).

So how can I solve this problem?
I need the possiblitiy before the CreateChildControl is called by the
control-base-class to have an information IsEdit true or not.
But ViewState and ControlState both are available later.

Thank you for any help.
Rolf Welskes
 
W

Walter Wang [MSFT]

Hi Rolf,

According to ASP.NET page life cycle, I would recommend you to always
create the TextBox and Label in CreateChildControls(); and hide the TextBox
or Label in PreRender according to the IsEdit property:

public class Class1: CompositeControl
{
public bool IsEdit
{
get
{
object o = ViewState["IsEdit"];
if (o == null) return false;
return (bool)o;
}
set { ViewState["IsEdit"] = value; }
}

TextBox tx;
Label lbl;

protected override void CreateChildControls()
{
Controls.Clear();
tx = new TextBox();
Controls.Add(tx);
lbl = new Label();
Controls.Add(lbl);
}

protected override void OnPreRender(EventArgs e)
{
tx.Visible = IsEdit;
lbl.Visible = !IsEdit;
if (lbl.Visible) lbl.Text = tx.Text;
base.OnPreRender(e);
}
}

#ASP.NET Page Life Cycle Overview
http://msdn2.microsoft.com/en-us/library/ms178472.aspx


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
thank you, I understand to do the decission in the render-function.
But the sample was simple. Reality is more complex.

In reality I have a table with 100 rows.
a property RowNumber which is the row where editboxes should be. 4 editboxes
in the row
the row witch RowNumber says.
Means the table has 100 rows and for example at rownumber 23 there are 4
cells with editboxes (instead normal text).

In your solution this would mean I had to build a controls-tree with 100*4 =
400 editboxes
and only 4 are visibible set when rendering.

So I think that is not good.

The 4 editBoxes should be set only in the control-tree where they are at
rowNumber row.

But how to do this?
Because I cannot get the RowNumber from the ViewState before calling
CreateChildControls().

Thank your for any suggestion.
Rolf Welskes



Walter Wang said:
Hi Rolf,

According to ASP.NET page life cycle, I would recommend you to always
create the TextBox and Label in CreateChildControls(); and hide the
TextBox
or Label in PreRender according to the IsEdit property:

public class Class1: CompositeControl
{
public bool IsEdit
{
get
{
object o = ViewState["IsEdit"];
if (o == null) return false;
return (bool)o;
}
set { ViewState["IsEdit"] = value; }
}

TextBox tx;
Label lbl;

protected override void CreateChildControls()
{
Controls.Clear();
tx = new TextBox();
Controls.Add(tx);
lbl = new Label();
Controls.Add(lbl);
}

protected override void OnPreRender(EventArgs e)
{
tx.Visible = IsEdit;
lbl.Visible = !IsEdit;
if (lbl.Visible) lbl.Text = tx.Text;
base.OnPreRender(e);
}
}

#ASP.NET Page Life Cycle Overview
http://msdn2.microsoft.com/en-us/library/ms178472.aspx


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your
reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.
 
W

Walter Wang [MSFT]

Hi Rolf,

Sorry for reply late.

You can recreate the child controls whenever the IsEdit property is changed:

set
{
if (ViewState["IsEdit"] != value)
{
ViewState["IsEdit"] = value;
CreateChildControls();
}
}

Regarding your example about a Table with one editing row, how about use a
GridView? A GridView supports editing by changing EditIndex directly. It
can use TemplateField's EditItemTemplate to represent the editing controls.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
thank you, it works fine.
Thank you again and best regards
Rolf Welskes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top