在回发ASP.Net复选框值是错的? [英] ASP.Net Checkbox value at postback is wrong?

查看:152
本文介绍了在回发ASP.Net复选框值是错的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个复选框,最初是禁用和检查。它然后通过JavaScript客户端启用。如果用户取消选中,然后在盒子和presses按钮来调用回传,复选框的状态保持在服务器端的检查。这显然​​是不可取的行为。下面是一个例子。

We have a checkbox that is initially disabled and checked. It is then enabled on the client side through javascript. If the user then unchecks the box and presses the button to invoke a postback, the state of the checkbox remains as checked on the server side. This is obviously undesirable behaviour. Here is an example.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testcb.aspx.cs" Inherits="ESC.testcb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <script type="text/javascript">
        function buttonClick() {
            var cb = document.getElementById('<%= CheckBox1.ClientID %>');
            cb.disabled = false;
            cb.parentNode.disabled = false;
        }


    </script>

    <div>
        <asp:CheckBox ID="CheckBox1" runat="server" Checked="true" Enabled="false" />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="buttonClick(); return false;" />
        <asp:Button ID="Button2" runat="server" Text="Button2" OnClick="button2Click" />
    </div>
    </form>
</body>
</html>

和Server端code:

And the Server-side code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ESC
{
    public partial class testcb : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void button2Click(object sender, EventArgs e)
        {
            string h = "";
        }
    }
}

因此​​,我们打破的串H行,并检查CheckBox1.Checked的价值。这是事实,即使是在表格上勾选。

So we break at the "string h" line and check the value of CheckBox1.Checked. It is true, even if it is unchecked on the form.

推荐答案

这是一个已知的问题与ASP.NET - 由于某种原因,如果页面加载,而不是在被禁用ASP.NET不会更新回发一个复选框检查回发。我不知道究竟这是为什么,虽然 - 如果你把默认不勾选复选框,并选择它,该值在服务器正确更改

This is a known problem with ASP.NET - for some reason ASP.NET won't update a checkbox on postback if it was disabled during page load and not checked for postback. I don't know exactly why that is though - if you make the checkbox unselected by default and select it, the value is changed on the server correctly.

解决方法是将一个隐藏字段添加到重新$ P $页面psents复选框的状态,然后更新字段的值设置为ON或OFF例如,当单击该复选框。

The workaround is to add a hidden field to the page that represents the state of the checkbox, then update the field's value to "ON" or "OFF" for example, whenever the checkbox is clicked.

然后在服务器上你检查隐藏字段,而不是复选框本身的价值,因为隐藏字段总是被公布。

Then on the server you check the value of the hidden field, not the checkbox itself, as the hidden field is always posted.

这篇关于在回发ASP.Net复选框值是错的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆