C# 使用for循环禁用多个按钮(特定类型的循环不重要)

C# 使用for循环禁用多个按钮(特定类型的循环不重要),c#,html,asp.net,C#,Html,Asp.net,在C#ASP.NET中,我有一个html页面,其中有30个按钮(按钮1,按钮2…按钮30)我想用一个循环禁用所有这些按钮 like按钮[i]。enable=false 我该怎么做? 我从数据库中读取了按钮列表,我想禁用那些具有(false)值的按钮 protected void Page_Load(object sender, EventArgs e) { DataSet.seatsDataTable seatDT = new DataSet.seatsDataTable();

C#
ASP.NET
中,我有一个
html
页面,其中有30个
按钮
(按钮1,按钮2…按钮30)我想用一个循环禁用所有这些按钮

like按钮[i]。enable=false

我该怎么做? 我从数据库中读取了
按钮列表,我想禁用那些具有(false)值的按钮

protected void Page_Load(object sender, EventArgs e) {
    DataSet.seatsDataTable seatDT = new DataSet.seatsDataTable();
    DataSetTableAdapters.seatsTableAdapter seatTA = new DataSetTableAdapters.seatsTableAdapter();
    seatTA.FillByAllSeatID(seatDT);
    for(int i = 1; i < 29; i++) {
        seatTA.FillBySeatIDfromUser(seatDT, i);
        if (seatDT[0].SeatStatus == false) {
            reserved.Text += ( seatDT[0].SeatID.ToString() + ",") ;
            //Here i want to change button disable/enable status.
            //like Button[i].enabled=false
        }
    }
}
受保护的无效页面加载(对象发送方,事件参数e){
DataSet.seatsDataTable seatDT=新的DataSet.seatsDataTable();
DataSetTableAdapters.seatsTableAdapter seatTA=新的DataSetTableAdapters.seatsTableAdapter();
seatTA.FillByAllSeatID(seatDT);
对于(int i=1;i<29;i++){
seatTA.FillBySeatIDfromUser(seatDT,i);
if(seatDT[0]。SeatStatus==false){
reserved.Text+=(seatDT[0].SeatID.ToString()+“,”);
//这里我想更改按钮禁用/启用状态。
//like按钮[i]。enabled=false
}
}
}
HTML代码:

   <section id="banner">
 <table class="auto-style1" style="align-self: center">
                    <tr>
                        <td>
                            <input id="Button1" type="button" runat="server" value="1" onclick="disable(1)" /></td>
                        <td>
                            <input id="Button2" type="button" runat="server" value="2" onclick="disable(2)" /></td>
                        <td>
                            <input id="Button3" type="button" value="3" runat="server" onclick="disable(3)" /></td>
                        <td>
                            <input id="Button4" type="button" value="4" runat="server" onclick="disable(4)" /></td>
                        <td>&nbsp;</td>
                        <td>
                            <input id="Button5" type="button" value="5" runat="server" onclick="disable(5)" /></td>
                        <td>
                            <input id="Button6" type="button" value="6" runat="server" onclick="disable(6)" /></td>
                        <td>
                            <input id="Button7" type="button" value="7" runat="server" onclick="disable(7)" /></td>
                        <td>
                            <input id="Button8" type="button" value="8" runat="server" onclick="disable(8)" /></td>
                        <td>
                            <input id="Button9" type="button" value="9" runat="server" onclick="disable(9)" /></td>
                    </tr>
                    <tr>
                        <td>
                            <input id="Button10" type="button" value="10" runat="server" onclick="disable(10)" /></td>
                        <td>
                            <input id="Button11" type="button" value="11" runat="server" onclick="disable(11)" /></td>
                        <td>
                            <input id="Button12" type="button" value="12" runat="server" onclick="disable(12)" /></td>
                        <td>
                            <input id="Button13" type="button" value="13" runat="server" onclick="disable(13)" /></td>
                        <td>
                            <input id="Button14" type="button" value="14" runat="server" onclick="disable(14)" /></td>
                        <td>
                            <input id="Button15" type="button" value="15" runat="server" onclick="disable(15)" /></td>
                        <td>
                            <input id="Button16" type="button" value="16" runat="server" onclick="disable(16)" /></td>
                        <td>
                            <input id="Button17" type="button" value="17" runat="server" onclick="disable(17)" /></td>
                        <td>
                            <input id="Button18" type="button" value="18" runat="server" onclick="disable(18)" /></td>
                        <td>
                            <input id="Button19" type="button" value="19" runat="server" onclick="disable(19)" /></td>
                    </tr>
                    <tr>
                        <td>
                            <input id="Button20" type="button" value="20" runat="server" onclick="disable(20)" /></td>
                        <td>
                            <input id="Button21" type="button" value="21" runat="server" onclick="disable(21)" /></td>
                        <td>
                            <input id="Button22" type="button" value="22" runat="server" onclick="disable(22)" /></td>
                        <td>
                            <input id="Button23" type="button" value="23" runat="server" onclick="disable(23)" /></td>
                        <td>
                            <input id="Button24" type="button" value="24" runat="server" onclick="disable(24)" /></td>
                        <td>
                            <input id="Button25" type="button" value="25" runat="server" onclick="disable(25)" /></td>
                        <td>
                            <input id="Button26" type="button" value="26" runat="server" onclick="disable(26)" /></td>
                        <td>
                            <input id="Button27" type="button" value="27" runat="server" onclick="disable(27)" /></td>
                        <td>
                            <input id="Button28" type="button" value="28" runat="server" onclick="disable(28)" /></td>
                        <td>
                            <input id="Button29" type="button" value="29" runat="server" onclick="disable(29)" /></td>
                    </tr>

                </table>


页面。您需要的是FindControl

    for (int i = 1; i < 10; i++)
    {
        Button b = this.FindControl("button" + i) as Button;
        if (b != null)
            b.Enabled = false;
    }

Page.FindControl是您需要的

    for (int i = 1; i < 10; i++)
    {
        Button b = this.FindControl("button" + i) as Button;
        if (b != null)
            b.Enabled = false;
    }

看起来你所有的按钮都在一张桌子里

好的,让我们禁用该表中的所有按钮

首先在您的表上放置一个特定的id,如下所示:

<table id="buttons-tbl" class="auto-style1" style="align-self: center">

然后,将此脚本放在表的底部

<script type="text/javascript">
    $("#buttons-tbl input[type=button]").prop("disabled", true);
</script>

$(“#按钮tbl输入[类型=按钮]”).prop(“已禁用”,true);

当然,您需要在html中包含jquery库,似乎所有按钮都在一个表中

好的,让我们禁用该表中的所有按钮

首先在您的表上放置一个特定的id,如下所示:

<table id="buttons-tbl" class="auto-style1" style="align-self: center">

然后,将此脚本放在表的底部

<script type="text/javascript">
    $("#buttons-tbl input[type=button]").prop("disabled", true);
</script>

$(“#按钮tbl输入[类型=按钮]”).prop(“已禁用”,true);

当然,您需要在html中包含jquery库。对不起,只是键入错误。仍然不知道该怎么做。您在问题中标记了javascript和jquery,您不想使用它吗?您可以使用单个jquery禁用所有按钮line@leo_ap我该怎么做?发布视图html,这样我们就可以看到你的按钮结构。有了它,我们可以为jquery创建一个选择器。这是您的按钮列表吗?对不起,只是键入错误。仍然不知道该怎么做。您在问题中标记了javascript和jquery,您不想使用它吗?您可以使用单个jquery禁用所有按钮line@leo_ap我该怎么做?发布视图html,这样我们就可以看到你的按钮结构。有了它,我们可以为jquerywhere创建一个选择器您的按钮列表?这是有效的,但是因为我使用的是HTML按钮,所以命令找不到控件。我怎么能修好它?好的,我修好了,你可以看到。天哪!thanx很多朋友你帮了很多忙。这很有效,但是因为我使用HTML按钮,所以命令找不到控件。我怎么能修好它?好的,我修好了,你可以看到。天哪!thanx很多朋友你帮了很多忙。