Dynamically creating an asp.net text field with the .CssClass attribute

I have a scenario where I have to dynamically create a form based on user selection. There are several text fields on the form that should be added to the end in Total Textbox.

The way I distinguish between text fields added at the end is indicated below:

TextBox txt1 = new TextBox(); txt1.ID = "txt1"; txt1.CssClass = "addToTotal"; TextBox txt2 = new TextBox(); txt2.ID = "txt2"; txt2.CssClass = "addToTotal"; TextBox txt3 = new TextBox(); txt3.ID = "txt3"; txt3.CssClass = "txtTotalPoints"; PlaceHolder1.Controls.Add(txt1); PlaceHolder1.Controls.Add(txt2); PlaceHolder1.Controls.Add(txt3); 

In fact, the site css file class does not have a css class named 'addToTotal' . It is simply used as a flag to notify me of an addition at the end.

It is good practice to add .CssClass, although the actual class does not exist. Are there any flaws in using this methodology?

+4
source share
1 answer

I would suggest that the overhead of using a CSS class that does not exist as a marker is minimal, so I would not change your implementation based on this. If you're worried about best practices that you could be fair, CSS classes were never meant to be used that way - you could add the data-* attribute for input and use this instead:

 txt2.Attributes["data-addToTotal"] = "true"; 

... then finding those elements with jQuery:

 $("input[data-addToTotal='true']") 

data-* attributes are part of HTML5 , but are fully backward compatible.

0
source

All Articles