在gridview中无法显示男性/女性的性别

时间:2014-05-29 11:59:37

标签: c# asp.net gridview

我有一个数据字段"性别" (sql数据类型 - 位)。 我创建了使用if条件绑定数据以检查true->男性否则 - >女。但它仍然没有显示出来。 这是aspx和代码隐藏代码:gridview显示true / false而不是male / female:

<%@ Page Title="Add User" Language="C#" AutoEventWireup="true" CodeFile="adduser.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<!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 id="Head1" runat="server">
    <title></title>
    <link href="style.css" type="text/css" rel="Stylesheet" />
    <script type="text/javascript">
        function confirmDelete() {
            return confirm("Do you want to delete this record?");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <ajax:ToolkitScriptManager ID="toolkit1" runat="server">
    </ajax:ToolkitScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <table border="0" align="center" cellpadding="2" cellspacing="2" class="maindiv">
                <tr><!--Hidden field for EmployeeID reference-->
                    <td colspan="2"><asp:HiddenField ID="txtHiddenEmpID" Value="0" runat="server" /></td>
                </tr>
                <tr>
                    <td>
                        <span class="asterisk">*</span><asp:Label ID="name" runat="server" Text="Name"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtEmpName" runat="server" CssClass="box"></asp:TextBox>
                        <asp:RequiredFieldValidator Display="None" ID="RequiredFieldValidator1" ErrorMessage="Name is required!"
                            EnableClientScript="true" SetFocusOnError="true" runat="server" ControlToValidate="txtEmpName"
                            CssClass="error_msg"></asp:RequiredFieldValidator>
                        <asp:RegularExpressionValidator ID="NameValidator" runat="server" ErrorMessage="Name can not contain numeric or special characters."
                            ControlToValidate="txtEmpName" ValidationExpression="^[A-Za-z ]*$" CssClass="error_msg"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        <span class="asterisk">*</span><asp:Label ID="Label1" runat="server" Text="Address"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="addressBox" runat="server" CssClass="box"></asp:TextBox>
                        <asp:RequiredFieldValidator Display="None" ID="AddressValidator" ErrorMessage="Address is required!"
                            EnableClientScript="true" SetFocusOnError="true" runat="server" ControlToValidate="addressBox"
                            CssClass="error_msg"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        <span class="asterisk">*</span><asp:Label ID="Label2" runat="server" Text="DOB"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="dobBox" runat="server" CssClass="dob_cal box" ReadOnly="false" ></asp:TextBox>
                        <ajax:CalendarExtender ID="CalenderExtender1" TargetControlID="dobBox" Format="dd/MM/yyyy"
                            runat="server">
                        </ajax:CalendarExtender>
                        <asp:RequiredFieldValidator Display="None" ID="dobValidator" ErrorMessage="DOB is required!"
                            EnableClientScript="true" SetFocusOnError="true" runat="server" ControlToValidate="dobBox"
                            CssClass="error_msg"></asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        <span class="asterisk">*</span><asp:Label ID="Label3" runat="server" Text="Salary"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="salaryBox" runat="server" CssClass="box" MaxLength="8" ></asp:TextBox>
                        <asp:RequiredFieldValidator ID="salaryValidate" runat="server" ControlToValidate="salaryBox"
                            ErrorMessage="Salary is required!" Display="None" CssClass="error_msg" SetFocusOnError="true"
                            EnableClientScript="true"></asp:RequiredFieldValidator>
                        <asp:RegularExpressionValidator ID="SalaryValidator" runat="server" ErrorMessage="Salary can contain only numeric values."
                            Display="None" ControlToValidate="salaryBox" ValidationExpression="^[0-9]*$"
                            CssClass="error_msg"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        <span class="asterisk">*</span><asp:Label ID="gender" runat="server" Text="Gender"></asp:Label>
                    </td>
                    <td>
                        <span>
                            <asp:RadioButton GroupName="gendergrp" ID="gendermale" runat="server" Text="Male"
                                Checked="true" /></span><span style="padding-left: 5px;">
                            <asp:RadioButton GroupName="gendergrp" ID="genderfemale" runat="server" Text="Female"
                                        Checked="false" /></span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div style="float: right; margin-right: -70px;">
                            <asp:Button ID="Button1" runat="server" CssClass="btn" Text="Save" OnClick="Button1_Click" /></div>
                    </td>
                    <td>
                        <div style="float: left; margin-left: 70px;">
                            <asp:Button ID="CancelBtn" CausesValidation="false" runat="server" CssClass="btn"
                                Text="Cancel" OnClick="CancelBtn_Click" />

                        </div>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Label ID="lblError" runat="server" CssClass="error_msg"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:ValidationSummary ID="valSum" DisplayMode="BulletList" EnableClientScript="true"
                            HeaderText="Error!" runat="server" CssClass="error_msg" />
                    </td>
                </tr>
            </table>
            <!--div for data display-->
            <div class="data_display">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                    DataKeyNames="EmployeeID" EmptyDataText="There are no data records to display."
                    GridLines="Horizontal" BackColor="#CCCCCC" BorderColor="White" Font-Bold="False"
                    Font-Names="Arial" Font-Size="Medium" ForeColor="#666666" 
                    AllowPaging="True" PageSize="5" PagerSettings-Mode="Numeric" 
                    PagerSettings-Position="Bottom" 
                    onpageindexchanging="GridView1_PageIndexChanging"
                    >
                    <Columns>
<%--                    <asp:TemplateField>
                        <ItemTemplate>
                        <asp:HiddenField ID="EmpID" runat="server" Value='<%# Eval("EmployeeID") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>

--%>                        <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID"  
                            SortExpression="EmployeeID" Visible="true" ItemStyle-HorizontalAlign="Center">
                        <ItemStyle HorizontalAlign="Center" />
                        </asp:BoundField>
                        <asp:BoundField DataField="EmployeeName" HeaderText="Name" SortExpression="EmployeeName"
                            ItemStyle-Width="130px" ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" Width="130px" />
                        </asp:BoundField>
                            <asp:BoundField DataField="DateOfBirth" HeaderText="DoB" SortExpression="DateOfBirth"
                            DataFormatString="{0:dd-MM-yyyy}" ItemStyle-Width="100px" ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" Width="100px" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Salary" HeaderText="Salary" 
                            SortExpression="Salary">
                            <ItemStyle HorizontalAlign="Center" Width="90px" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Gender" HeaderText="Gender" ItemStyle-Width="90px"
                            ItemStyle-HorizontalAlign="Center">
                            <ItemStyle HorizontalAlign="Center" Width="70px" />
                        </asp:BoundField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="EditBtn" runat="server" Text="Select" CausesValidation="false" OnClick="EditBtn_Click" />
                                <asp:Button  ID="DelBtn" runat="server" Text="Delete" CausesValidation="false" OnClick="DelBtn_Click" OnClientClick="confirmDelete()" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#ffffff" />
                    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="Gray" Font-Bold="false" ForeColor="White" />
                    <PagerStyle BackColor="Gray" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#E3EAEB" />
                    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F8FAFA" />
                    <SortedAscendingHeaderStyle BackColor="#246B61" />
                    <SortedDescendingCellStyle BackColor="#D4DFE1" />
                    <SortedDescendingHeaderStyle BackColor="#15524A" />
                </asp:GridView>
                <br />
                <span style="font-family: Arial; font-size: small; color: Green; font-weight: bold;">
                    You are viewing page <%=GridView1.PageIndex + 1%> of <%=GridView1.PageCount%>
                </span>
            </div>
            <div>
                <asp:Label ID="lblMessage" runat="server"></asp:Label></div>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

**code behind:**


    #region GridView Functions
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[4].Text == "True")
            {
                e.Row.Cells[4].Text = "Male";
            }
            else
            {
                e.Row.Cells[4].Text = "Female";

            }
        }
    }
#endregion

gridview Gender列显示true / false而不是male / female。 谢谢!

1 个答案:

答案 0 :(得分:3)

您需要TemplateField。假设Male等于True,这应该有效:

<asp:TemplateField HeaderText="Gender" SortExpression="Gender">
    <ItemTemplate><%# (Boolean.Parse(Eval("Gender").ToString())) ? "Male" : "Female" %></ItemTemplate>
</asp:TemplateField>