I have ASP.NET MVC view page that has Checkbox(Active) and a button(Activate).
Here are somethings I want to do:
If the value from the database is True, The checkbox should be Checked and Enabled and so button also should be Enabled.开发者_如何转开发
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
精彩评论