5

I am using/abusing CSS classes and custom html attributes to provide default data to a set of textboxes. The code-front for this looks like the following (with some supporting javascript to handle checking/setting the default data when the field is blank):

<asp:TextBox ID="TXT_LenderName" class='defaultText' data-default='Institution Name' runat="server"></asp:TextBox>

This works.

I am working on the code-behind to process this form. I would like to be able to compare the value of the TXT_LenderName.Text to the value of the data-default attribute, but I haven't been able to find a way to get the value of a custom html attribute. Suggestions?

2
  • 2
    Why don't you use the defaultValue instead? Commented Oct 8, 2012 at 16:50
  • DefaultValue would be a good way to do this as well, and at the time of this post, I was not aware of that attribute on text box controls. Am I correct to assume that this attribute is not available to the browser once the control is rendered? Commented Nov 30, 2012 at 22:49

3 Answers 3

11

This is tested and worked

string customAttrDataDefault = TXT_LenderName.Attributes["data-default"];
txtpassword.Attributes.Add("value","Password value");
Sign up to request clarification or add additional context in comments.

1 Comment

Is the .ToString() actually necessary for any reason? .Attributes is already of type string from what I can tell.
4

try this:

TXT_LenderName.Attributes["AttributeName"]= value;//here get or set the value.

1 Comment

I had to use () instead of [] like this TXT_LenderName.Attributes("AttributeName") in order to work. Did I miss something?
4

If the control, like the TextBox control inherits from the System.Web.UI.WebControls.Control class then it should have an Attributes property which is a name value pair collection of the control's attributes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.