How to extend control to add custom html tag ?

Discussion in 'ASP .Net' started by timor.super@gmail.com, Dec 18, 2007.

  1. Guest

    Hi group,

    the aspx statement :
    <asp:TextBox ID="testTB" runat="server" Text="My test" />
    generate html :
    <input name="testTB" type="text" value="My test" id="testTB" />

    Imagine I want to create a custom web control, extending the TextBox
    control to add a custom hidden value.
    Something like :

    <MyNamespace:MyTextBox ID="testTB" runat="server" Text="My test"
    HiddenValue="a value" />

    that can generate such html code :
    <input name="testTB" type="text" value="My test" id="testTB" />
    <input name="hid_testTB" type="hidden" value="a value"
    id="hid_testTB" />

    How can I render such html code ? (without having to rewritte all
    using writer.WriteBeginTag , and other writer.WriteAttribute)

    Something like

    protected override void ?? (...)
    {
    RenderTheBaseControl();
    RenderTheAddedHiddenField();
    }

    How can I do ?

    Thanks in advance for your help.

    S.
     
    , Dec 18, 2007
    #1
    1. Advertising

  2. Mark Rae [MVP], Dec 18, 2007
    #2
    1. Advertising

  3. Guest

    I want my own custom control to be able to add both TextBox control
    and HiddenField, not adding 2 control in my page.

    This is an example, maybe later I would like to do something
    different, but I would like to understand how I can override the
    render by adding html code

    On 18 déc, 11:53, "Mark Rae [MVP]" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    > > How can I render such html code?

    >
    > http://msdn2.microsoft.com/en-us/library/ms228301(VS.80).aspx
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net
     
    , Dec 18, 2007
    #3
  4. <> wrote in message
    news:...

    On 18 déc, 11:53, "Mark Rae [MVP]" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    >>> How can I render such html code?

    >>
    >> http://msdn2.microsoft.com/en-us/library/ms228301(VS.80).aspx
    >>

    > I want my own custom control to be able to add both TextBox control
    > and HiddenField, not adding 2 control in my page.


    Apologies, but I don't understand what the problem is...

    You can have as many controls as you like in your UserControl - see examples
    here:
    http://www.15seconds.com/issue/020319.htm

    If you want to create a UserControl which has a TextBox and a HiddenField,
    then create a UserControl with a TextBox and a HiddenField - when you add it
    to your page and set the various properties, it will render the HTML you
    want...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Dec 18, 2007
    #4
  5. Guest

    Thanks for your answer, but I don't want to use UserControl because I
    need some specific uses, such as RaiseCallbackEvent.
    That's why I was thinking of a custom webcontrols, inheriting from
    TextBox
    And somewhere in a render method, render the TextBox and render the
    HiddenField, but I don't know how

    You see ?



    On 18 déc, 12:26, "Mark Rae [MVP]" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    > On 18 déc, 11:53, "Mark Rae [MVP]" <> wrote:
    >
    > > <> wrote in message

    >
    > >news:...

    >
    > >>> How can I render such html code?

    >
    > >>http://msdn2.microsoft.com/en-us/library/ms228301(VS.80).aspx

    >
    > > I want my own custom control to be able to add both TextBox control
    > > and HiddenField, not adding 2 control in my page.

    >
    > Apologies, but I don't understand what the problem is...
    >
    > You can have as many controls as you like in your UserControl - see examples
    > here:http://www.15seconds.com/issue/020319.htm
    >
    > If you want to create a UserControl which has a TextBox and a HiddenField,
    > then create a UserControl with a TextBox and a HiddenField - when you add it
    > to your page and set the various properties, it will render the HTML you
    > want...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net
     
    , Dec 18, 2007
    #5
  6. Hello Timor

    Iam not sure wether i understood or not ;-)
    Its possible to do something like:

    protected override Render(object sender, HtmlTextWriter writer) {
    // Render the TextBox (Base Control)
    base.Render(writer);

    // Render the HiddenField
    this.RenderMyHiddenField(writer);
    }

    protected void RenderMyHiddenField(HtmlTextWriter writer) {
    // Render....
    }

    In your new methode, you can render the hiddenfield using the "writer"
    object.

    --
    Gruss, Peter Bucher
    Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
     
    Peter Bucher [MVP], Dec 18, 2007
    #6
  7. <> wrote in message
    news:...

    > Thanks for your answer, but I don't want to use UserControl because I
    > need some specific uses, such as RaiseCallbackEvent.
    > That's why I was thinking of a custom webcontrols, inheriting from
    > TextBox
    > And somewhere in a render method, render the TextBox and render the
    > HiddenField, but I don't know how
    >
    > You see ?


    No, I'm afraid I don't... There's nothing preventing you from using
    RaiseCallbackEvent in a UserControl - here's an example of how to do it with
    a UserControl which contains two ListBox controls:
    http://www.c-sharpcorner.com/Upload...111212006084429AM/Callback_ListBoxesFT_1.aspx


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Dec 18, 2007
    #7
  8. Guest

    Thanks both for your help,

    The Peter's solution helps me to understand that it's very simple to
    play with writer.
    Mark's solution help me reconsidering what I want.

    I think I will find what I want

    Thanks for your help.

    Best regards,



    On 18 déc, 14:07, "Mark Rae [MVP]" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    > > Thanks for your answer, but I don't want to use UserControl because I
    > > need some specific uses, such as RaiseCallbackEvent.
    > > That's why I was thinking of a custom webcontrols, inheriting from
    > > TextBox
    > > And somewhere in a render method, render the TextBox and render the
    > > HiddenField, but I don't know how

    >
    > > You see ?

    >
    > No, I'm afraid I don't... There's nothing preventing you from using
    > RaiseCallbackEvent in a UserControl - here's an example of how to do it with
    > a UserControl which contains two ListBox controls:http://www.c-sharpcorner..com/UploadFile/LivMic/Callback_ListBoxesFT_1...
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net
     
    , Dec 18, 2007
    #8
  9. It isn't really clear from your post what the goal is, but it's very easy to
    add attributes to any ASP.NET control:

    this:
    protected void Page_Load(object sender, EventArgs e)
    {
    this.TextBox1.Attributes.Add("hiddenField", "blahblah");
    }

    would render like this:

    <input name="TextBox1" type="text" id="TextBox1" hiddenField="blahblah" />
    --Peter
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    MetaFinder: http://www.blogmetafinder.com


    "" wrote:

    > Hi group,
    >
    > the aspx statement :
    > <asp:TextBox ID="testTB" runat="server" Text="My test" />
    > generate html :
    > <input name="testTB" type="text" value="My test" id="testTB" />
    >
    > Imagine I want to create a custom web control, extending the TextBox
    > control to add a custom hidden value.
    > Something like :
    >
    > <MyNamespace:MyTextBox ID="testTB" runat="server" Text="My test"
    > HiddenValue="a value" />
    >
    > that can generate such html code :
    > <input name="testTB" type="text" value="My test" id="testTB" />
    > <input name="hid_testTB" type="hidden" value="a value"
    > id="hid_testTB" />
    >
    > How can I render such html code ? (without having to rewritte all
    > using writer.WriteBeginTag , and other writer.WriteAttribute)
    >
    > Something like
    >
    > protected override void ?? (...)
    > {
    > RenderTheBaseControl();
    > RenderTheAddedHiddenField();
    > }
    >
    > How can I do ?
    >
    > Thanks in advance for your help.
    >
    > S.
    >
     
    Peter Bromberg [C# MVP], Dec 18, 2007
    #9
    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. =?Utf-8?B?ams=?=
    Replies:
    6
    Views:
    680
    =?Utf-8?B?ams=?=
    Dec 24, 2004
  2. shruds
    Replies:
    1
    Views:
    894
    John C. Bollinger
    Jan 27, 2006
  3. Rolf Welskes
    Replies:
    3
    Views:
    474
    Rolf Welskes
    Oct 6, 2006
  4. Replies:
    1
    Views:
    412
    Andrew Thompson
    Nov 29, 2006
  5. henrique de souza

    Controlling html/xml tag of custom control from control code

    henrique de souza, Apr 23, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    147
    henrique de souza
    Apr 23, 2004
Loading...

Share This Page