checkBox禁用文本框

来源:互联网 发布:浩格云信企业数据服务 编辑:程序博客网 时间:2024/05/02 00:18

 

前台:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
    <script language="javascript" type="text/javascript">
        function Check()
        {
            var d = document.forms(0);
            if (d.ckDemo.checked)
            {
                document.getElementById("txtDemo").disabled = false;
            }
            else
            {
                document.getElementById("txtDemo").disabled = true;
            }
        }

        function Recheck()
        {
            Check();
        }
    </script>
</head>
<body onload="Check();">
    <form id="form1" runat="server">
    <asp:CheckBox ID="ckDemo" runat="server" onclick ="Recheck();" />
        <asp:TextBox ID="txtDemo" runat="server"></asp:TextBox>
    </form>
</body>
</html>

 

后台:

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.ckDemo.Text = "勾中启用,取消勾中禁用文本框";
        this.ckDemo.Checked = true;//假设这里是你在后台里给复选按钮设置选中,或者未选中状态
    }
}