问题
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?
回答1:
This is tested and worked
string customAttrDataDefault = TXT_LenderName.Attributes["data-default"];
txtpassword.Attributes.Add("value","Password value");
回答2:
try this:
TXT_LenderName.Attributes["AttributeName"]= value;//here get or set the value.
回答3:
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.
来源:https://stackoverflow.com/questions/12785946/how-can-i-access-custom-textbox-attributes-in-asp-net