1

I have a scenario where I need to conditionally slide up (or hide) sets of controls/elements. I like the answer I found here from QBM5 about assigning data-groups to the controls that may be slid up (pardon my grammar) like so:

<input data-group="1" type="text" />

...but again, I am creating these controls via C#, not pure/straight HTML, so I need to add that data-group property to it somehow programmatically. But this attempt to do so with a TextBox (input, type=text):

boxRequesterName = new TextBox
{
    CssClass = "finaff-webform-field-input"
};
boxRequesterName.DataGroup = 1; // Nice try, but there is no such property as "DataGroup"
cellTxtbx_1.Controls.Add(boxRequesterName);
dynamicTable.Rows.Add(row1);

...won't work, because there is no "DataGroup" property on the TextBox.

What can I do to add the equivalent of data-group="1" to my control?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

2

First off, please notice that DataGroup or data-group is nothing predefined, it's an HTML5 data-* attribute which can be named anything you like.

Since you are working with ASP.NET and WebForms, you can use the Attributes collection on your TextBox element like this:

boxRequesterName.Attributes.Add("data-group", "1");

See on MSDN WebControl.Attributes and AttributeCollection.

MarioDS
  • 12,895
  • 15
  • 65
  • 121