如何在只读表单上显示复选框(ASP.NET)

发布于 2024-11-05 07:25:01 字数 150 浏览 5 评论 0原文

我有一个从数据库自动填写的只读表单。我有几个带有真/假值的字段,并希望它们显示为空或已选中的复选框。我可以对复选框控件进行数据绑定并禁用它,但随后它会显示为灰色。有没有一种简单的方法可以改变它,使其以正常、易于阅读的粗体显示,但仍然被禁用?如果没有,最好的方法是什么?我应该使用图像吗?

I have a read-only form filled out automatically from a database. I have several fields with true/false values, and would like them displayed as checkboxes that are either empty or checked. I can databind the checkbox control and disable it, but then it appears grayed out. Is there a simple way to change this so it will show up at a normal, easy-to-read boldness but still be disabled? If not, what's the best way to do this? Should I use an image?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

负佳期 2024-11-12 07:25:01

你也可以这样走。

<asp:CheckBox id="checkbox1" Text="Custom" onclick="javascript: return false;" />

这会将其呈现为

<input type="checkbox"
checked  onclick="javascript: return false;">Custom</input>

You can go like this as well.

<asp:CheckBox id="checkbox1" Text="Custom" onclick="javascript: return false;" />

Which will render it as

<input type="checkbox"
checked  onclick="javascript: return false;">Custom</input>
抽个烟儿 2024-11-12 07:25:01

有趣的问题!

使用图像而不是普通复选框时,我只能看到一个问题,这就是活动复选框与禁用(基于图像)复选框的不同之处。因此,如果您选择图像路线,您可能会想要为所有复选框设置样式。 :)

您可以使用以下 jQuery 方法有效地禁用任何复选框(到目前为止,我只在 Chrome 和 IE 9 中进行了测试)。

$('.readonly:checkbox').click(function(e) {
    e.preventDefault();
});

这将“禁用”任何具有“readonly”类的复选框。

或者,您可以使用内联 JavaScript 函数(尽管不推荐,因为它会增加混乱和混淆):

<input type="checkbox" onclick="event.preventDefault();" />

您使用“preventDefault()”而不是“return false”的原因是因为返回 false 将停止传播。这意味着您的点击事件不会冒泡到父元素。这可能会导致其他代码出现问题……但可能性不大。

另外,就像 Bala R 提到的那样,您不能依赖这些限制来工作。您的服务器端代码应该知道这些值是只读的,并且不要更新它们。

这是 jsFiddle 示例:http://jsfiddle.net/xixionia/3rXCB/

我希望这有帮助! :)

Interesting question!

There's only one issue I can see with using images instead of normal checkboxes, and that's how the active checkboxes will differ from the disabled (image-based) checkboxes. So, if you go the image route, you will probably want to style all checkboxes. :)

You can effectively disable any checkbox with the following jQuery method (I've only tested in Chrome and IE 9, so far).

$('.readonly:checkbox').click(function(e) {
    e.preventDefault();
});

This will "disable" any checkbox with the "readonly" class.

Or, you can use an inline JavaScript function (albiet not recommended, as it adds clutter and confusion):

<input type="checkbox" onclick="event.preventDefault();" />

The reason you would use "preventDefault()" instead of "return false", is because returning false will stop propagation. This means that your click event will not bubble to the parent element. That could potentially cause problems with other code... but it's unlikely.

Also, like Bala R mentioned, you mustn't rely on these restrictions to work. Your server side code should be aware that these values are read only, and refrain from updating them.

Here is jsFiddle example: http://jsfiddle.net/xixionia/3rXCB/

I hope this is helpful! :)

滿滿的愛 2024-11-12 07:25:01

您可以使用 javascript 执行类似的操作,并将其用于复选框的 onclick 处理程序。

jsFiddle 链接

function makeMeReadonly(checkbox){
    checkbox.checked = !checkbox.checked;
}

<asp:CheckBox id="checkbox1" onclick="makeMeReadonly(this)" />

/或

<asp:CheckBox id="checkbox1" onclick="this.checked = !this.checked" />

如果您希望将其内联。

但您不应该依赖于此来确保安全,因为当 javascript 不可用和/或客户端代码并不难解决客户端代码限制时,它将是一个常规复选框,但在您的情况下,因为您有只读视图,我不会认为你有任何逻辑来保存更改。

You can do something like this with javascript and use it for the checkbox's onclick handler.

jsFiddle link

function makeMeReadonly(checkbox){
    checkbox.checked = !checkbox.checked;
}

and

<asp:CheckBox id="checkbox1" onclick="makeMeReadonly(this)" />

or

<asp:CheckBox id="checkbox1" onclick="this.checked = !this.checked" />

if you want it inline.

but you shouldn't depend on this for security as it will be a regular check box when javascript is not available and/or client code is not that difficult to work around client code restrictions but in your case since you have a readonly view, I wouldn't think you have any logic to save changes.

厌味 2024-11-12 07:25:01

我刚刚遇到了同样的问题,并通过复选框上的 OnClick 事件修复了它。在事件处理程序中,.Checked 状态设置为它应该显示的值。

private void chkBox_CheckStateChanged(object sender, EventArgs e)
    {
        chkBox.Checked = boolDatabasevalue;
        return;
    }

效果很好,用户可以单击复选框,但复选标记不会改变。

唯一的问题是指示焦点的轻微边框阴影。但正如OP所寻找的那样,勾号是易于阅读的粗体。

注意:这是 C#,但在 Asp.Net 中应该类似地工作。

I just had the same problem and fixed it with an OnClick event on the checkbox. In the event handler the .Checked state is set to the value it is supposed to display.

private void chkBox_CheckStateChanged(object sender, EventArgs e)
    {
        chkBox.Checked = boolDatabasevalue;
        return;
    }

Works great, the user can click on the checkbox but the check mark does not change.

The only issue is a slight border shadow indicating the Focus. But the checkmark is, as OP was looking for, of easy-to-read boldness.

NB: this is C#, but in Asp.Net this should work similarly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文