Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当复选框未选中时,如何使GridView中的此行为灰色?_C#_Asp.net_Gridview_Checkbox - Fatal编程技术网

C# 当复选框未选中时,如何使GridView中的此行为灰色?

C# 当复选框未选中时,如何使GridView中的此行为灰色?,c#,asp.net,gridview,checkbox,C#,Asp.net,Gridview,Checkbox,我有以下数据库设计: Employee Table: Username, Name, JobTitle, BadgeNo, IsActive, DivisionCode Divisions Table: SapCode, DivisionShortcut 我有一个GridView,我正在使用它添加、删除和更新/编辑员工信息。此信息是员工用户名、姓名、徽章号、职务、IsActive和部门快捷方式IsActive是一个标志,指示员工是否可用或是否在工作分配中。我将其设置为复选框,该列应显示两个值;

我有以下数据库设计:

Employee Table: Username, Name, JobTitle, BadgeNo, IsActive, DivisionCode
Divisions Table: SapCode, DivisionShortcut
我有一个GridView,我正在使用它添加、删除和更新/编辑员工信息。此信息是员工用户名、姓名、徽章号、职务、IsActive和部门快捷方式IsActive是一个标志,指示员工是否可用或是否在工作分配中。我将其设置为复选框,该列应显示两个值;活跃和不活跃。在编辑模式下,将显示复选框。如果选中,则表示该员工可用,否则该员工处于非活动状态

我编写了代码,一切正常,但现在我只面临一个问题,那就是:当复选框未选中时,这意味着员工处于非活动状态,因此我希望显示其信息的行为灰色(如禁用)

那么怎么做呢?

ASP.NET代码:

<%-- GridView for User Management Subsystem --%>
        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
            AutoGenerateColumns="False" DataKeyNames="Username" 
            DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" BorderWidth="1px" BackColor="#DEBA84" 
             CellPadding="3" CellSpacing="2" BorderStyle="None" 
             BorderColor="#DEBA84">
            <FooterStyle ForeColor="#8C4510" 
              BackColor="#F7DFB5"></FooterStyle>
            <PagerStyle ForeColor="#8C4510" 
              HorizontalAlign="Center"></PagerStyle>
            <HeaderStyle ForeColor="White" Font-Bold="True" 
              BackColor="#A55129"></HeaderStyle>
            <Columns>
                <asp:CommandField ButtonType="Image" ShowEditButton="true" ShowCancelButton="true"
                                EditImageUrl="Images/icons/edit24.png" UpdateImageUrl="Images/icons/update24.png" 
                                CancelImageUrl="Images/icons/cancel324.png" />

                <asp:TemplateField HeaderText="Division">
                    <ItemTemplate>
                        <%# Eval("DivisionShortcut")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DivisionsList" runat="server" DataSourceID="DivisionsListDataSource"
                                          DataTextField="DivisionShortcut" DataValueField="SapCode"
                                          SelectedValue='<%# Bind("DivisionCode")%>'>
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:BoundField DataField="Username" HeaderText="Network ID" ReadOnly="True" 
                    SortExpression="Username" />

                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <%# Eval("Name")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEmployeeName" runat="server" Text='<%# Bind("Name")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Job Title">
                    <ItemTemplate>
                        <%# Eval("JobTitle")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtJobTitle" runat="server" Text='<%# Bind("JobTitle")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Badge No.">
                    <ItemTemplate>
                        <%# Eval("BadgeNo")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtBadgeNo" runat="server" Text='<%# Bind("BadgeNo")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Is Active?">
                    <ItemTemplate>
                        <asp:Label ID="lblIsActive" runat="server" Text='<%# Eval("IsActive")%>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:CheckBox ID="isActive" runat="server" 
                                      AutoPostBack="true" OnCheckedChanged="isActive_OnCheckedChanged"
                                      Checked='<%# Convert.ToBoolean(Eval("IsActive")) %>'
                                      Text='<%# Eval("IsActive")%>'/>
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Delete?">
                    <ItemTemplate>
                        <span onclick="return confirm('Are you sure to Delete the record?')">
                            <asp:ImageButton ID="lnkB" runat="server" ImageUrl="Images/icons/delete24.png" CommandName="Delete" />
                        </span>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

在复选框未选中状态的方法中,您将设置GrdiViewRow的背景色,如下所示
gvrow.BackColor=Color.Gray

一个重要的想法是,在第一次加载时,您需要重复GridView的OnRowDataBound的类似逻辑,以便将其移入方法并重用

protected void isActive_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkStatus = (CheckBox)sender;
        GridViewRow gvrow = (GridViewRow)chkStatus.NamingContainer;

        //Get the ID which is the NetworkID of the employee
        string username = gvrow.Cells[2].Text;
        bool status = chkStatus.Checked;
if(!status)//this is checkbox is unchecked then set backcolor to Gray
{
gvrow .BackColor = Color.Gray;
}

.......

我不明白你的意思。你能再解释一下吗?它不起作用。为什么?是因为我已经在ASP.NET代码中定义了GridView的背景色,如上面的代码所示。它不起作用。为什么?是否因为我已经在ASP.NET代码中定义了GridView的背景色,如上面的代码所示。可能是,但我不这么认为?通过从gv上拆下进行检查。
protected void isActive_OnCheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkStatus = (CheckBox)sender;
        GridViewRow gvrow = (GridViewRow)chkStatus.NamingContainer;

        //Get the ID which is the NetworkID of the employee
        string username = gvrow.Cells[2].Text;
        bool status = chkStatus.Checked;
if(!status)//this is checkbox is unchecked then set backcolor to Gray
{
gvrow .BackColor = Color.Gray;
}

.......
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
    // First check Checkedbox is check or not, In Cells insert you IsActive column index
    if( e.Row.Cells[1].Text == "False" )
        GridView1.BackColor = Color.Gray;

 }
}