How to extend control to add custom html tag ?

T

timor.super

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.
 
T

timor.super

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
 
M

Mark Rae [MVP]

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...
 
T

timor.super

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 ?
 
P

Peter Bucher [MVP]

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

Mark Rae [MVP]

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
 
T

timor.super

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,
 
P

Peter Bromberg [C# MVP]

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
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top