Welcome to AspAdvice Sign in | Join | Help

@ASP.NET Forums: How to set text to a Password TextBox?

My serie of all sorts of hacks continues... :-)

As per subject, it was asked at Forums how to set the text to a password mode TextBox such that you could populate it from database, for example. Point is that ASP.NET TextBox does not care about the Text property when it is in the Password mode. It just posts the value once but doesn't keep it over the postback by default. If you want to get the feature, understand that this changes it so that password will be carried along with postbacks. However, here you go.

The first option

Just add it to the Attributes collection of TextBox with "value" as key.

[C#]
TextBox1.Attributes["value"]="I'm set";

[VB]
TextBox1.Attributes("value")="I'm set"

The second option

You can also write this out as a custom control, which derives from TextBox and adds the proper logic to carry the value around. This way you could continue using the TextBox as you generally do via the Text property, no need to care about when adding attribute and when not.

[C#]

using System;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Joteke.Controls
{

	public class MyTextBox : TextBox
	{

		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender (writer);
			if(this.TextMode == TextBoxMode.Password)
			{
				string text=this.Text;
				if(text.Length > 0)
					writer.AddAttribute(HtmlTextWriterAttribute.Value,text);   
			}
		}

	}
}


[VB] (translated from C# code)

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Joteke.Controls

   Public Class MyTextBox
      Inherits TextBox

      Protected Overrides Sub AddAttributesToRender(writer As HtmlTextWriter)
         MyBase.AddAttributesToRender(writer)

        If Me.TextMode = TextBoxMode.Password Then
            Dim [text] As String = Me.Text

            If [text].Length > 0 Then
               writer.AddAttribute(HtmlTextWriterAttribute.Value, [text])
            End If

         End If

      End Sub 'AddAttributesToRender 

   End Class 'MyTextBox

End Namespace 'Joteke.Controls
EDIT - code for developing same with HtmlInputText (needs custom control)
public class MyHtmlInputText : System.Web.UI.HtmlControls.HtmlInputText 
	{
		protected override void RenderAttributes(HtmlTextWriter writer)
		{
			string val=base.Attributes["value"] as string; 
			if(val != null)
			{
				writer.WriteAttribute("value",val,false); 
			}

			base.RenderAttributes(writer);
		}
	}
Thanks
Sponsor
Published Thursday, February 03, 2005 10:41 PM by joteke
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: @ASP.NET Forums: How to set text to a Password TextBox?

Nice solutions...i use the first one (because it simply just takes less time and i've rarely had to do it).

but i think i might just create the second option as part of the component library i'm creating for work right now.

cheers on that..
Saturday, February 19, 2005 5:59 PM by joteke

# re: @ASP.NET Forums: How to set text to a Password TextBox?

this solution would work for TextBox control but would not work for HtmlInputText control! I have been using HtmlInputText controls and had to resort to client-side java script to set the value; nothing on the server worked for me.
Thursday, March 31, 2005 8:05 PM by joteke

# re: @ASP.NET Forums: How to set text to a Password TextBox?

That's not a problem all means, because HtmlInputText uses Attributes collection straight, it needs a bit different approach. I've updated code snippet into this post.
Thursday, March 31, 2005 9:17 PM by joteke

# re: @ASP.NET Forums: How to set text to a Password TextBox?

Great!
Thank you very much for your elegant solution.
Thursday, December 22, 2005 3:57 PM by Ulon

# re: @ASP.NET Forums: How to set text to a Password TextBox?

Thanks a lot! Your workaround have been very useful for me!
Wednesday, January 11, 2006 9:26 PM by Pascal P.

# re: @ASP.NET Forums: How to set text to a Password TextBox?

Simply briljant ... I don't know who's bug we're workingaround  ... but your solution simply works ... briljant!!!

Paul

ps. I took option I, since places in my code where i'd need this workaround are always handling password goodies anyways ... I can handle the extra 1 line.

Thursday, June 15, 2006 10:46 AM by paul

# re: @ASP.NET Forums: How to set text to a Password TextBox?

Thanks very much! I've been looking for the solution for a long time.
Monday, July 24, 2006 6:34 PM by Warren

# re: @ASP.NET Forums: How to set text to a Password TextBox?

Thanks, option 1 worked like a charm!
Saturday, December 20, 2008 4:51 PM by Andrew

# re: @ASP.NET Forums: How to set text to a Password TextBox?

Thank you! :) But I think this is only a good solution for an app which has no high security priority!? Someone told me, never send a password back to the page (in any kind of way). But for my app it´s perfect. Thx.
Tuesday, April 21, 2009 11:18 AM by Patrick

# re: @ASP.NET Forums: How to set text to a Password TextBox?

As I said: "If you want to get the feature, understand that this changes it so that password will be carried along with postbacks. However, here you go." :-)

Tuesday, April 21, 2009 12:12 PM by joteke

# re: @ASP.NET Forums: How to set text to a Password TextBox?

Thank You Very Much, u saved much of my time
Tuesday, July 14, 2009 2:13 AM by Chandralekha.N.S

# re: @ASP.NET Forums: How to set text to a Password TextBox?

Ver Good solution .Perfectly working Great Hip hip Hurey Thanks .
Tuesday, August 04, 2009 12:24 AM by rakesh

# re: @ASP.NET Forums: How to set text to a Password TextBox?

Thank you so much, you saved my day.
Wednesday, October 21, 2009 4:37 AM by aziz

# re: @ASP.NET Forums: How to set text to a Password TextBox?

I see it's been 2 months since someone said thank you, so Thanks!
Sunday, December 27, 2009 4:01 PM by Michael Washington

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below