Problems with webcontrol and MultiView

Discussion in 'ASP .Net Web Controls' started by Mike W, May 4, 2006.

  1. Mike W

    Mike W Guest

    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
     
    Mike W, May 4, 2006
    #1
    1. Advertising

  2. 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.
     
    Walter Wang [MSFT], May 5, 2006
    #2
    1. Advertising

  3. Mike W

    Mike W Guest

    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

    "Walter Wang [MSFT]" <> skrev i en meddelelse
    news:NGbBhj$...
    > 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.
    >
     
    Mike W, May 5, 2006
    #3
  4. 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.
     
    Walter Wang [MSFT], May 8, 2006
    #4
  5. Mike W

    Mike W Guest

    That works! Thank you very much

    -Michael
     
    Mike W, May 9, 2006
    #5
  6. 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.
     
    Walter Wang [MSFT], May 10, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    2
    Views:
    757
  2. Andy B
    Replies:
    0
    Views:
    397
    Andy B
    Dec 29, 2008
  3. Luis Ramírez.

    Can a webcontrol (webcontrol.dll) have a configuration file?

    Luis Ramírez., Jul 5, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    221
    Luis Ramírez
    Jul 6, 2004
  4. Kasabaarde Sumta

    WebControl inside of WebControl and attaching events

    Kasabaarde Sumta, Dec 13, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    215
    Kasabaarde Sumta
    Dec 13, 2004
  5. Fabricio

    Custom webcontrol that references another webcontrol

    Fabricio, Sep 21, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    206
    Fabricio
    Sep 21, 2005
Loading...

Share This Page