CheckBox oncheckedchanged事件 - 复选框检查时可见按钮

时间:2014-09-11 11:45:49

标签: gridview checkbox oncheckedchanged

我遇到CheckBox oncheckchanged事件的问题。

复选框位于gridview中,如果选中该复选框,我想要一个按钮可见。

如果选中一个或多个复选框但是取消选中时刻1复选框,则按钮变为可见,即使选中了另一个复选框,该按钮也会变为不可见。

请求帮助

<asp:Button ID="BtnRequestCartons" runat="server" Text="Request Cartons" 
            BorderStyle="Ridge"  onclick="BtnRequestCartons_Click" Visible="False" />



 <asp:GridView ID="GridViewcrtreq" runat="server" AllowPaging="False"    
            AutoGenerateColumns="False" DataSourceID="SqlDataSource5" CellPadding="0" 
            ForeColor="#333333" GridLines="None" PageSize="5" BorderColor="#CCCCCC" 
            BorderStyle="Solid" BorderWidth="1px"  ShowFooter="true" Width="100%" 
            HeaderStyle-CssClass="gvHeader" 
            >
            <AlternatingRowStyle BackColor="white" ForeColor="#284775" />

            <Columns>

            <asp:TemplateField>

            <HeaderTemplate> 


    <tr class="gvHeader" align="right"> 
       <th style="width:0px"></th> 
       <th colspan="3">

       </th>   
    </tr> 
    <tr class="gvHeader"> 
      <th></th> 
      <th>
      <asp:CheckBox ID="chkSelectAll" runat="server" Text="" onclick="javascript:SelectAllCheckboxes(this);"/>

      </th> 
      <th align="left">Carton ID</th> 
      <th align="left">Carton Status</th> 
    </tr> 




            </HeaderTemplate>


        <ItemTemplate>

           <asp:CheckBox ID="chkSelectAdd" OnCheckedChanged="GridViewcrtreq_OnCheckedChanged" runat="server" AutoPostBack="True" />

            <td align="left"><%# Eval("CartonID")%></td> 
    <td  align="left"><%# Eval("CartonStatus")%></td> 


            </ItemTemplate> 

           <FooterTemplate>



    </FooterTemplate>


    </asp:TemplateField>


</Columns>


            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="White" Font-Bold="True" ForeColor="Black" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1" />
            <HeaderStyle BackColor="#D6D6C2" Font-Bold="True" ForeColor="Black" HorizontalAlign="Center" BorderWidth="1" BorderStyle="Solid" BorderColor="#999999" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" BorderColor="Silver" BorderWidth="1" BorderStyle="Solid" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>



protected void GridViewcrtreq_OnCheckedChanged(object sender, EventArgs e)
    {

          CheckBox chkSelectAdd = sender as CheckBox;

        if (chkSelectAdd.Checked == true)
        {
            BtnRequestCartons.Visible = true;
        }
        if (chkSelectAdd.Checked == false)
        {
            BtnRequestCartons.Visible = false;
        }


    }

2 个答案:

答案 0 :(得分:0)

最简单的方法是检查每个CheckBoxCell,看看每次检查其中一个单元的检查状态是否有任何变化。

以下代码假定您的CheckBoxColumn是第一列。如果CheckBoxColumn不是第一列(索引:0),则在 .Cells [>之后更改0以匹配CheckBoxColumn的列索引。

protected void GridViewcrtreq_OnCheckedChanged(object sender, EventArgs e){
    CheckBox chkSelectAdd = sender as CheckBox;
    if (chkSelectAdd.Checked == true){
        BtnRequestCartons.Visible = true;
    }else{
        foreach (DataGridViewRow row in dataGridView1.Rows) {
            button1.Enabled = false;
            if ((bool)row.Cells[0].Value == true) {
                BtnRequestCartons.Enabled = true;
                break;
            }
        }
    }
}

答案 1 :(得分:0)

尝试此代码。...

  1. 如果至少选中一个复选框,则该按钮可见,而所有未选中的复选框均不可见

这是我的html代码:

<form id="form1" runat="server">
    <div>

        <asp:Button ID="btnchkvisible" runat="server" Text="visible" Visible="false" />

        <asp:GridView ID="grvsch" runat="server" AutoGenerateColumns="false" BorderColor="#808081" BorderStyle="Solid" BorderWidth="1" Width="99.5%" Style="margin-left: 2px; margin-right: 2px; margin-top: 2px;" ShowHeader="true" ShowFooter="false"
            GridLines="None">
            <RowStyle VerticalAlign="Middle" HorizontalAlign="Center" />
            <HeaderStyle VerticalAlign="Middle" HorizontalAlign="Center" />
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:Label ID="lblhdoseno" runat="server" Text="Dose Number"></asp:Label>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chk" runat="server" OnCheckedChanged="chk_CheckedChanged" AutoPostBack="true" />

                        <asp:Label ID="lbldoseno" runat="server" Text='<%#Eval("date") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


    </div>
</form>

这是

后面的代码
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dtnew = new DataTable();
            dtnew.Columns.Add("date");


            for (int i = 0; i < 5; i++)
            {
                var rows = dtnew.NewRow();
                rows["date"] = DateTime.UtcNow.AddDays(i);
                dtnew.Rows.Add(rows);

            }

            grvsch.DataSource = dtnew;
            grvsch.DataBind();
        }
    }

protected void chk_CheckedChanged(object sender, EventArgs e)
{
    bool onechk = false; 
    foreach (GridViewRow row in grvsch.Rows)
    {
        CheckBox chkbox = (CheckBox)row.FindControl("chk");

        if (chkbox.Checked)
        {
            onechk = true;
            break;
        }
        else
        {
        }
    }

    if (onechk == true)
    {
        btnchkvisible.Visible = true;
    }
    else
    {
        btnchkvisible.Visible = false;
    }
}