2

我们尝试使用 C# 将 checkBox1 链接到 txtCommentBox。我们正在尝试使 txtCommentBox 保持禁用状态,直到 checkBox1 被选中。

我们已经完成了以下操作。

if (checkBox1.Enabled)
{
    txtCommentBox.Enabled = true;
}

失败后,我们在 page_Load 方法中尝试执行以下操作。

txtCommentBox.Enabled = checkBox1.Enabled;

那也没有用。我们用控件尝试了各种属性,但没有成功。.aspx 代码在下面,C# 代码在下面。

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

<!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>Information</title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 605px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <h4>
            If you would like to leave your Questions, Comments, E-mail, Name or Phone 
            Number check off the box you would like to enter into the form.
        </h4>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <table class="style1">
            <tr>
                <td class="style2">
                    <asp:Label ID="Label1" runat="server"><span class="accesskey">C</span>omment:</asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:TextBox ID="txtCommentBox" runat="server" AccessKey="C" Width="334px" 
                        ontextchanged="txtCommentBox_TextChanged" Enabled="False"></asp:TextBox>
                    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack = "true"
                        oncheckedchanged="CheckBox1_CheckedChanged" Text="Enable/disable" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Label ID="Label2" runat="server"><span class="accesskey">E</span>mail:</asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:TextBox ID="txtEmailBox" runat="server" AccessKey="E" Width="334px" 
                        ontextchanged="txtEmailBox_TextChanged" Enabled="False"></asp:TextBox>
                    <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack = "true"
                        oncheckedchanged="CheckBox2_CheckedChanged" Text="Enable/disable" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Label ID="Label3" runat="server"><span class="accesskey">N</span>ame:</asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:TextBox ID="txtNameBox" runat="server" AccessKey="N" Width="334px" 
                        ontextchanged="txtNameBox_TextChanged" Enabled="False"></asp:TextBox>
                    <asp:CheckBox ID="CheckBox3" runat="server" AutoPostBack = "true"
                        oncheckedchanged="CheckBox3_CheckedChanged" Text="Enable/disable" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Label ID="Label4" runat="server"><span class="accesskey">P</span>hone Number:</asp:Label>
                    <asp:TextBox ID="txtPhoneBox" runat="server" AccessKey="P" Width="334px" 
                        ontextchanged="txtPhoneBox_TextChanged" Enabled="False"></asp:TextBox>
                    <asp:CheckBox ID="CheckBox4" runat="server" AutoPostBack = "true"
                        oncheckedchanged="CheckBox4_CheckedChanged" Text="Enable/disable" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
        <asp:ListBox ID="ListBox1" runat="server" Height="321px" Width="887px">
        </asp:ListBox>
        <br />
        <br />
        <br />
        <br />

        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="Back to the Main Page" />
        <br />

    </div>
    </form>
</body>
</html>

我已经将我们的 C# 代码放在下面:

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

public partial class Infomation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtCommentBox.Enabled = false;
            txtEmailBox.Enabled = false;
            txtNameBox.Enabled = false;
            txtPhoneBox.Enabled = false;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("JoelsDefaultPage.aspx");
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Enabled == true)
            txtCommentBox.Enabled = true;
        else
            txtCommentBox.Enabled = false;
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox2.Enabled == true)
            txtEmailBox.Enabled = true;
        else
            txtEmailBox.Enabled = false;
    }
    protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox3.Enabled == true)
            txtNameBox.Enabled = true;
        else
            txtNameBox.Enabled = false;
    }
    protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox4.Enabled == true)
            txtPhoneBox.Enabled = true;
        else
            txtPhoneBox.Enabled = false;
    }
    protected void txtCommentBox_TextChanged(object sender, EventArgs e)
    {

    }
    protected void txtEmailBox_TextChanged(object sender, EventArgs e)
    {

    }
    protected void txtNameBox_TextChanged(object sender, EventArgs e)
    {

    }
    protected void txtPhoneBox_TextChanged(object sender, EventArgs e)
    {

    }
}

编辑#1

好的,所以 AutoPostBack 有点工作。我现在可以从禁用状态启用文本框,但是我不能再次禁用它。

更新#1

我已经将代码更新为我们现在所拥有的。

4

5 回答 5

4

我认为问题出在这条线上

<asp:CheckBox ID="CheckBox1" runat="server"  
     oncheckedchanged="CheckBox1_CheckedChanged" Text="Enable/disable" />

它缺少 autopostback="true"
结合它将是

<asp:CheckBox ID="CheckBox1" runat="server"  AutoPostBack="True" 
     oncheckedchanged="CheckBox1_CheckedChanged" Text="Enable/disable" />

编辑-1

这是相同
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkbox.autopostback.aspx的 MSDN 示例

编辑-2

但我会建议你使用java-scriptjQuery相同。
这是一个很好的例子

使用复选框和 jQuery 禁用/启用元素?

于 2013-01-29T04:36:39.067 回答
3

这对我有用。也许你在所有的事件中都错过了一个 else 。正如@MsB 指出的那样,每当您想回发到服务器时,您还需要将复选框的 AutoPostBack 属性设置为 true ...检查下面的示例(也更新了我的答案以通过 javascript 启用/禁用)

   <form id="form1" runat="server">
    <div>
        <h2>Enable/Disable via checkbox server side</h2>
        <p>
        <label>Name:</label>
        <asp:TextBox ID="txtName" runat="server" Enabled="false"></asp:TextBox>
        <asp:CheckBox ID="cbEnableName" runat="server" AutoPostBack="true" 
                Text="Enable Name" oncheckedchanged="cbEnableName_CheckedChanged" />
        </p>

        <h2>Enable/Disable via checkbox client side</h2>
        <p>
        <label>Address:</label>
        <asp:TextBox ID="txtAddress" runat="server" Enabled="false"></asp:TextBox>
        <asp:CheckBox ID="cbEnableAddress" runat="server" onclick="EnableDisableAddress()"
                Text="Enable Address" />
        </p>

    </div>

    <script type="text/javascript">
        function EnableDisableAddress() {
            var chkAddress = document.getElementById('<%=cbEnableAddress.ClientID %>');
            var txtAddress = document.getElementById('<%= txtAddress.ClientID %>');

            txtAddress.disabled = !chkAddress.checked;
        }
    </script>
</form>

protected void cbEnableName_CheckedChanged(object sender, EventArgs e)
    {
        if (cbEnableName.Checked)
            txtName.Enabled = true;
        else
            txtName.Enabled = false;
    }
于 2013-01-29T04:57:52.817 回答
3

要检查复选框的状态,您要使用

textbox.Enabled = checkBox.Checked;

而不是复选框的启用状态。

于 2013-01-29T16:49:40.970 回答
2
<asp:CheckBox ID="CheckBox1" runat="server"  AutoPostBack="True" 
                    oncheckedchanged="CheckBox1_CheckedChanged" Text="Enable/disable" />

您必须将“AutoPostBack”设置为 true,以便在选中复选框时,它将重新加载页面并在 if 语句中运行您的命令。

于 2013-01-29T04:42:21.217 回答
0
protected void EnableTextBox()
{
    int count = int.Parse(GridView1.Rows.Count.ToString());

    for (int i = 0; i < count; i++)
    {
        CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
        CheckBox cb1 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox2");
        CheckBox cb2 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox3");
        TextBox tb = (TextBox)GridView1.Rows[i].Cells[4].FindControl("txtration");
        TextBox tb1 = (TextBox)GridView1.Rows[i].Cells[5].FindControl("txtjob");
        TextBox tb2 = (TextBox)GridView1.Rows[i].Cells[6].FindControl("txtaadhar");

        if (cb.Checked == true)
        {
            tb.Visible = true;
        }
        else
        {
            tb.Visible = false;
        }
        if (cb1.Checked == true)

        {
            tb1.Visible = true;
        }
        else
        {
            tb1.Visible = false;
        }
        if (cb2.Checked == true)

        {
            tb2.Visible = true;
        }
        else
        {
            tb2.Visible = false;
        }
    }
}

protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
    EnableTextBox();
}
于 2014-12-19T10:01:14.920 回答