1

I have ASP.NET MVC view page that has Checkbox(Active) and a button(Activate).

Here are somethings I want to do:

  1. If the value from the database is True, The checkbox should be Checked and Enabled and so button also should be Enabled.

  2. Else If the value from the database is False, The chekbox should not be Checked and Disabled. And so button should be disabled.

Here is the View Code:

<% using (Html.BeginForm("ActivateUser", "Home", FormMethod.Post, new { id = "frmActivate" })) 
   {%>
            <%= Html.Hidden("pwdEmail")%>
            <input type="hidden" id="isLocked" value="<%= ViewData["isLocked"]%>" />
            <table><tbody>
                <tr><td class="Form_Label"><label for="chkActive">Active</label></td>
                <td><%= Html.CheckBox("chkActive", false)%></td> <td><input type="submit" value="Activate" disabled="disabled" /></td></tr>
            </tbody></table>
<% } %>

Appreciate your responses.

Thanks

1
  • You want the controller and repository code? Commented Feb 2, 2010 at 18:43

1 Answer 1

1

You could try extending the HtmlHelper with a method like this:

    public static string CheckBox(this HtmlHelper htmlHelper, string name, bool checked, bool enabled)
    {
        TagBuilder builder = new TagBuilder("input");

        builder.Attributes.Add("type", "checkbox");
        builder.Attributes.Add("name", name);
        builder.Attributes.Add("id", name);

        if (checked)
            builder.Attributes.Add("checked", "checked");

        if (disabled)
            builder.Attributes.Add("disabled", "disabled");

        return builder.ToString();
    }

Then you can call this extension method on your MVC Page

4
  • I should mention that from experience, disabling any control isn't a good way of validating or securing a page. Most browsers allow you to edit the html and someone could easily remove the "disabled" attribute and submit. Commented Feb 2, 2010 at 19:10
  • Yup. You are right Hunter. This is the internal application only. I am going to try now and update you shortly. Commented Feb 2, 2010 at 19:32
  • This is not working for me. I need this logic from JQuery function. Commented Feb 3, 2010 at 4:55
  • oh, I didn't see any jquery tags in your question. What are you trying to do with jquery? if you want to see if something is checked you can do $("#input[type=checkbox]").is(":checked"); Commented Feb 3, 2010 at 16:57

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.