【问题标题】:Conditional clickbox in asp.net c# gridviewasp.net c# gridview中的条件点击框
【发布时间】:2016-06-24 09:55:55
【问题描述】:

我是 Asp.net 和 C# 的新手,但我需要帮助我的老板设计一些 Web 表单。 下面是我设计一个大型网络表单供用户输入帐户并单击复选框的代码。如何让人们点击第一个复选框,然后第二个复选框也会被选中?

请告诉我什么样的问题可以帮助您理解我想要完成的任务。谢谢!

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="Service_Solution" %>
 <asp:GridView ID="GridView1" runat="server">
    <Columns> 
        <asp:TemplateField>
            <HeaderTemplate>
                <th colspan="30">Key Account Desk Service Solutions</th>
                <tr>
                <th style="width:0px"></th>
                <th></th>
                <th></th>
                <th colspan="4">Shipment Monitoring</th>
                <th colspan="2">Shipment Tracing</th>

                </tr>                   
           <tr>
                    <th></th>
                    <th class="auto-style8">Customer Name/Location</th>
                    <th class="auto-style8">Account Number</th>
                    <th class="auto-style8">100% Real Time Proactive Monitoring  through QCC and QSMS</th>
                    <th class="auto-style8">100% Proactive Monitoring through NPTS</th>


                </tr>             
            </HeaderTemplate>
        </asp:TemplateField>

        <asp:TemplateField>
            <ItemTemplate>
                  <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                  <asp:TextBox ID="txtNo" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                  <asp:CheckBox ID="CB1" runat="server"  />
            </ItemTemplate>
        </asp:TemplateField>
     <asp:TemplateField>
            <ItemTemplate>
                  <asp:CheckBox ID="CB2" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>    
    </Columns>
</asp:GridView>

【问题讨论】:

    标签: c# asp.net gridview checkbox conditional


    【解决方案1】:

    在标记中设置事件:

    <asp:CheckBox ID="CB1" runat="server" OnCheckedChanged="CB1_CheckedChanged" />
    

    您可以使用 NamingContainer 属性在两个复选框之间建立连接:

    protected void CB1_CheckedChanged(Object sender, EventArgs e)
    {
        CheckBox cb1 = sender as CheckBox;
        Control container = cb1.NamingContainer;
        CheckBox cb2 = container.FindControl("CB2") as CheckBox;
        cb2.Checked = cb1.Checked;
    }
    

    【讨论】:

      【解决方案2】:

      将此添加到第一个复选框:

      OnCheckedChanged="Check_Clicked"/>
      

      以及方法:

      void Check_Clicked(Object sender, EventArgs e) 
      {
           CB2.Checked = CB1.Checked;
      }
      

      【讨论】:

      • 嗨@theanathema。谢谢您的答复。在我的 Check_Clicked 中找不到 CB1 和 CB2,我只在我的选项中看到 Check_Clicked。
      猜你喜欢
      • 1970-01-01
      • 2021-04-19
      • 2010-09-24
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      相关资源
      最近更新 更多