Problems with webcontrol and MultiView

M

Mike W

Hi

I have a problem with my webcontrol when I use in a MultiView.

I have a fairly simple webcontrol that consists of a Table with one row and
two cells, in the first Cell i have two TextBoxe's (one of which is hidden
from the User) and in the second Cell i have a HtmlButton.

The code is:

namespace WorkTech
{
[ParseChildren(false), ToolboxData("<{0}:SelectBox runat=server />")]
public class SelectBox : System.Web.UI.WebControls.WebControl,
INamingContainer
{
private TextBox m_Textbox;
private TextBox m_TextboxHiddenID;
private string m_Javascript = string.Empty;
public string JavaScript
{
get { return m_Javascript; }
set { m_Javascript = value; }
}
public string Text
{
get { return m_Textbox.Text; }
set { m_Textbox.Text = value; }
}
public Guid HiddenID
{
get { return Functions.ParseGuid(m_TextboxHiddenID.Text); }
set { m_TextboxHiddenID.Text = value.ToString(); }
}
protected override void CreateChildControls()
{
Controls.Clear();
m_Textbox = new TextBox();
m_TextboxHiddenID = new TextBox();
Table myTable = new Table();
m_Textbox.ID = "Textbox";
m_Textbox.CssClass = "TextBox";
if (Width.Type.ToString() == "Pixel" && Width.Value > 0)
{
int TextBoxWidth = Convert.ToInt32((Width.Value - 22));
m_Textbox.Width = Unit.Pixel(TextBoxWidth);
}
m_Textbox.Attributes.Add("readonly", "true");
m_TextboxHiddenID.ID = "HiddenID";
m_TextboxHiddenID.CssClass = "Hidden";
myTable.CellPadding = 0;
myTable.CellSpacing = 0;
TableRow TR = new TableRow();
TableCell TD = new TableCell();
TD.VerticalAlign = VerticalAlign.Top;
TD.Controls.Add(m_Textbox);
TD.Controls.Add(m_TextboxHiddenID);
TR.Controls.Add(TD);
TD = new TableCell();
TD.VerticalAlign = VerticalAlign.Top;
TD.Width = Unit.Pixel(22);
HtmlButton myButton = new HtmlButton();
myButton.ID = "Button";
myButton.Attributes.Add("onclick", m_Javascript);
myButton.Attributes.Add("class", "SelectBoxButton");
TD.Controls.Add(myButton);
TR.Controls.Add(TD);
myTable.Controls.Add(TR);
Controls.Add(myTable);
}
/// <summary>
/// Override to prevent span
/// </summary>
/// <param name="writer"> The HTML writer to write out to </param>
public override void RenderBeginTag(HtmlTextWriter writer) { }
/// <summary>
/// Override to prevent span
/// </summary>
/// <param name="writer"> The HTML writer to write out to </param>
public override void RenderEndTag(HtmlTextWriter writer) { }
}
}

And the code on the .aspx pages is:
<WorkTech:SelectBox ID="PaymentReciever" Width="200px"
JavaScript="AddPaymentReciever();" runat="server" />

This works fine and supports postback. BUT when i use it in a MultiView with
2 or more Views like this:
<asp:MultiView ID="MultiView1" ActiveViewIndex="0" runat="server">
<asp:View ID="View1" runat="server">
Hello World!<br />
</asp:View>
<asp:View ID="View2" runat="server">
<WorkTech:SelectBox ID="PaymentReciever" Width="200px"
JavaScript="AddPaymentReciever();" runat="server" />
</asp:View>
<asp:View ID="View3" runat="server">
Hello World!<br />
</asp:View>
</asp:MultiView>

The my control looses data if I perfom a postback while View2 is not the
ActiveView.

Example's:
I'm in View2, I fill out the Textboxes, go to View1 and back to View2. That
works
If Im' in View2, fill out the Textboxes, go to View3, then to View1 and then
to View2. Then my data is lost?

I really hope you understand my problem, and that you can help me!

Thanks

Michael
 
W

Walter Wang [MSFT]

Hi Michael,

Thanks for your posting.

The recommended way to build a composite server control in ASP.NET 2.0 is
to inherit from CompositeControl. It implements INamingContainer for you,
and any time the controls collection is accessed (both run-time and
design-time), it calls EnsureChildControls() for you.

If you still want to inherit from WebControl and implement
INamingContainer, just override Controls property to make sure it calls
EnsureChildControls() first:

public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}

Actually this is the recommended way to build composite server controls in
ASP.NET 1.1.

If there is anything unclear, please feel free to post here.

Regards,

Walter Wang
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.
 
M

Mike W

Thanks for your answer!

Inheriting from CompositeControl almost works. If I do that, my control will
remember it's values between postbacks, but if load it with data like this:
if (!Page.IsPostBack)
{
PaymentReciever.Text = "Hello World!";
PaymentReciever.HiddenID = Guid.Empty;
}
the values will not be set if the control is in a hidden View.

I also tried to implement EnsureChildControls() and that works to, but if I
do that, then my JavaScript property looses its value!


-Michael
 
W

Walter Wang [MSFT]

Hi Michael,

Thanks for your update.

According to your initial sample code, you might need to call
EnsureChildControls() at the beginning of property "Text" and "HiddenID".

As for the property "JavaScript", you might need to save it in ViewState,
for example:

public string JavaScript
{
get {
object value = ViewState["JavaScript"];
if (value == null)
return string.Empty;
return (string)value;
}
set {
ViewState["JavaScript"] = value;
}
}

If there is anything unclear, please feel free to post here.

Regards,

Walter Wang
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.
 
W

Walter Wang [MSFT]

You're welcome.

Have a nice day!


Regards,

Walter Wang
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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top