A GrammerPro A GrammerPro - 2 months ago 6x
C# Question

ASP.Net Check Boxes and Labels

I have a checkbox that I am hiding on certain conditions. I have a label associated with each checkbox that is being populated based on a Boolean value. The problem I am facing is that I want to keep each label, but hide only the check boxes. I know I can make a label and just show it after I hide the checkbox. I am just curious as if there is a way to remove the checkbox but keep the text shown.

C# code:

if(some condition)
chkbox.Visible = false;
lblName.Text = Bool_Code == BooleanType.No ? "No" : "Yes";

Is there anything I can do to remove the lblName and just pull the "yes"/"no" text from each checkbox.. I am setting the lbls to checkboxes on databind.


If you set chkbox.Visible = false then the control is NOT rendered at all.

Now because the check box in asp.net is rendered with two html tags, you can do this.

Disable the checkbox for avoid to click on it in any casem, but still rendered and Hide the input control with style. To do the second part you use the InputAttributes that referred only on checkbox

    chkbox.Enabled = false;
    chkbox.InputAttributes["style"] = "display:none;";