开发者

enable/disable checkbox & button based on value from database on ASP.NET MVC View page?

开发者 https://www.devze.com 2022-12-19 12:01 出处:网络
I have ASP.NET MVC view page that has Checkbox(Active) anda button(Activate). Here are somethings I want to do:

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号