0

I have a dynamically created checkbox from code behind that has a long label on my webform asp.net project. When the text wraps to a new line the new line of text starts directly under the checkbox itself. How can I dynamically set the checkbox so the new line is automatically indented if needed. Not all dynamically created checkboxes have a long label.

Here is my code that creates the checkbox:

MyObject obj = SetNewObject("Supervisor"); 

CheckBox objCheck6 = new CheckBox();
objCheck6.ID = obj.GetFieldValue("Name");
objCheck6.Text = GetControlTitle("Supervisor");  
objCheck6.Checked = obj.GetFieldValue("Value").ToLower() == "true";
tableCell1.Controls.Add(objCheck6);

I have tried a couple things that have not worked. I tried the following, one at a time and each seemed to have zero effect on the checkbox or checkbox's text at all:

objCheck6.Style.Add("margin-left", "20px");

objCheck6.LabelAttributes.Add("margin-left", "20px");

objCheck6.TextAlign = TextAlign.Right;

objCheck6.TextAlign = TextAlign.Left;

A point in the right direction would be much appreciated. Thanks!

Icculus018
  • 1,018
  • 11
  • 19
  • Tip: Create the control just the way you want it, using ASP.NET web forms designer. Then go look at the markup and the codebehind that was generated to see what properties need to be set in your own code. – John Wu Aug 30 '18 at 16:30
  • Um ok, sure, i can create an asp:checkbox in the designer. The problem is that i am creating the checkbox dynamically. When a checkbox is created dynamically it creates not an asp:checkbox, but an input and label. I am asking how do i set those two differently created tags in the code behind. How do i set the indent on a dynamically created checkbox. I have been trying for a while and any guidance towards that issue would be much appreciated. Thanks. – Icculus018 Aug 30 '18 at 16:35

1 Answers1

1

Edit your css and add this so all the labels from the checkBoxes are affected.

label{
  display: block;
  margin-top: -18px;
  margin-left: 20px;
}

If this causes a problem because you have other labels in your page, name that css class and add it dynamically to your checkBox like this:

objCheck6.InputAttributes["class"] = "className";

UPDATE

This can also be achieved with the code mentioned in the comments:

objCheck6.LabelAttributes.Add("style", "display:block;margin-top:-18px;margin-left:20px;");

I used this question as a reference.

Pietro Nadalini
  • 1,722
  • 3
  • 13
  • 32
  • The checkbox is dynamically created so I assume you mean to add these lines of code to my code: `objCheck6.Style.Add("padding-left", "1.5em");` and `objCheck6.Style.Add("text-indent", "-1.5em");` which i did and it had no effect. Since they are dynamically created, I have no way to dynamically add to the css. – Icculus018 Aug 30 '18 at 16:45
  • 1
    Ultimately, your answer guided me to an answer. I came up with adding the following line of code to the code behind while creating the checkbox: `objCheck6.LabelAttributes.Add("style", "display:block;margin-top:-18px;margin-left:20px;");`. After adding this, it did exactly what i needed. Thank you! – Icculus018 Aug 30 '18 at 18:15