1

I have a webform with a checkbox in it. I need to do two things differently based on an environment setting.

  1. Add a class
  2. Add the Text attribute so a label gets created

                            <% if setting == true) { %>
                            <asp:CheckBox ID="optionCheckbox" class="option-checkbox radio-checkbox" runat="server" Text="Label Text"/>
                            <% } else { %>
                            <asp:CheckBox ID="optionCheckbox" class="option-checkbox" runat="server"/>
                            <% } %>
    

The problem with this is the page won't render because the ids are the same, even though only one could ever get rendered. There is a lot of other processing with javascript and such so I don't want different ids for each scenario.

2 Answers 2

1

I was able to fix this by keeping only the "base" checkbox code below and then overriding the PreRender event to add the class and set the Text attribute there.

<asp:CheckBox ID="optionCheckbox" class="option-checkbox" runat="server"/>

Code Behind:

                CheckBox optionCheckbox = this.optionCheckbox as CheckBox;
                if (optionCheckbox != null)
                {
                    optionCheckbox.Text = "Label Text";
                    optionCheckbox.Attributes.Add("class", "option-checkbox radio-checkbox");
                }

I'd still like to know if there is a way to do this in the markup file though.

Sign up to request clarification or add additional context in comments.

Comments

0

Sorry about this syntax , I don't use ASPNet webPage. You can implement it logically..

Firstly , define class and text variable and set value for business..

Finaly set checkbox class and Text value by variable

var class = "option-checkbox";
var text = "";
if(setting == true)
{
    class = "option-checkbox radio-checkbox";
    text  = "Label Text"; 
}

 <asp:CheckBox ID="optionCheckbox" class="setClassProperty" runat="server" Text="SetTextProperty"/>

1 Comment

This is similar to what I ended up doing but won't quite work as written. Thank you though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.