Solved

Extending Textbox capabilities

Posted on 2009-05-12
16
186 Views
Last Modified: 2013-11-08
Hi

I have Created a project of type "ASP.NET Server Control"

The class is inherited from WebControls

I have changed the base class to Textbox


How can I render the textbox in  RenderContents

protected override void RenderContents(HtmlTextWriter output)
0
Comment
Question by:KaranGupta
  • 8
  • 7
16 Comments
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24363377
use the HtmlTextWriter object to render an input tag with the type "text" like this:
	protected override void RenderContents(HtmlTextWriter writer)
	{
		//base.RenderContents(writer);
		writer.RenderBeginTag(HtmlTextWriterTag.Input);
		writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
		writer.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID);
		//etc...
 
		writer.RenderEndTag();
	}

Open in new window

0
 
LVL 9

Expert Comment

by:tetorvik
ID: 24363504
To get it work you'll need to override Render, not RenderContents method. Method content should be like GuitarRich provided.

protected override void Render(HtmlTextWriter writer)
{
...
}

Open in new window

0
 

Author Comment

by:KaranGupta
ID: 24372852
How can I get label in Render function
I have used the following code(code in code section)

but I am not getting label


output.RenderBeginTag(HtmlTextWriterTag.Label);                
output.AddAttribute(HtmlTextWriterAttribute.Value, "karan");                

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 12

Expert Comment

by:GuitarRich
ID: 24372884
did you render the end tag?
0
 

Author Comment

by:KaranGupta
ID: 24372907
Hi

I am using following code in Render function
output.RenderBeginTag(HtmlTextWriterTag.Label);
                output.AddAttribute(HtmlTextWriterAttribute.Value, "karan");      
                output.RenderEndTag();

Open in new window

0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24372933
did you comment out the
base.Render(writer)
line?
also try using the Write methods like this:

		writer.WriteBeginTag("label");
		writer.Write("karan");
		writer.WriteEndTag("label");

Open in new window

0
 

Author Comment

by:KaranGupta
ID: 24372944
where to comment base.Render

because as such there is no line as base.Render in the code

But I have overridden the render function
0
 

Author Comment

by:KaranGupta
ID: 24372963
When I write this code (code section)

I am getting a textbox
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
output.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID);
output.RenderEndTag();

Open in new window

0
 

Author Comment

by:KaranGupta
ID: 24372993
Hi

My class is inherited from Control class that is why I am getting this problem

when I am inheriting this class from TextBox class then it is working fine

May I know the reason for this
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24373136
do you want a composite control? ie a label combined with a textbox? if so it would probably be better to use a WebUserControl and drop the 2 controls in there rather than render them yourself.
0
 

Author Comment

by:KaranGupta
ID: 24373195
I want a custom control in which I want to use the dll further in the project
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24373281
and do you want that to be a label next to a textbox?
0
 

Author Comment

by:KaranGupta
ID: 24373299
Yes I want label next to textbox

and this label should be visible on a particular value of the property
0
 
LVL 12

Accepted Solution

by:
GuitarRich earned 500 total points
ID: 24373384
ok - this works in a control I have just tried - I think this is linked to your other question:
			//base.Render(writer);
			writer.RenderBeginTag(HtmlTextWriterTag.Span);
 
			writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
			writer.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID);
			writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
			writer.RenderBeginTag(HtmlTextWriterTag.Input);
 
			writer.RenderEndTag();
 
			if (this.MTBText != string.Empty && this.MTBErrorMessageControlType == MTBErrorMessageControl.LABEL)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.For, this.ClientID);
				writer.RenderBeginTag(HtmlTextWriterTag.Label);
 
				writer.Write(this.MTBText);
				writer.RenderEndTag();
			}
 
			writer.RenderEndTag(); // span

Open in new window

0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24373386
I have put all that in the Render overrided method.
0
 

Author Comment

by:KaranGupta
ID: 24373429
it works thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now