Reputation: 1076
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!
Upvotes: 0
Views: 631
Reputation: 1800
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.
Upvotes: 1