0

我有一个网格,下面是我为它编写的代码。在我的这个列的网格中,即<asp:BoundField DataField = "IsNOWEnabled" HeaderText = "Enabled/Disabled" HtmlEncode = "true"/>来自数据库的 1 或 0 即将到来。我希望在绑定时,网格将此列的文本 1 替换为启用,将 0 替换为禁用,这样当网格绑定时,它显示 1 为启用,0 为禁用列启用/禁用。

请指导我,我如何实现这一目标。

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <asp:GridView ID="GridView1" runat="server"  Width = "550px" AutoGenerateColumns = "false"  AlternatingRowStyle-BackColor = "#C2D69B" HeaderStyle-BackColor = "green" AllowPaging ="true" OnPageIndexChanging = "OnPaging" PageSize = "10" >
        <Columns>
            <asp:BoundField DataField = "NatureOfWorkID" HeaderText = "Nature Of WorkID" HtmlEncode = "true" Visible="false" />
            <asp:BoundField DataField = "NatureOFWorkName" HeaderText = "Nature Of Work Name"  HtmlEncode = "true" />    
            <asp:BoundField DataField = "IsNOWEnabled" HeaderText = "Enabled/Disabled"  HtmlEncode = "true"/> 
            <asp:TemplateField ItemStyle-Width = "30px"  HeaderText = "NatureOfWorkID">
               <ItemTemplate>
                   <asp:LinkButton ID="lnkEdit" runat="server" Text = "Edit" OnClick = "Edit"></asp:LinkButton>
               </ItemTemplate>
            </asp:TemplateField>
        </Columns> 
        <AlternatingRowStyle BackColor="#C2D69B"  />
    </asp:GridView>
    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick = "Add" />
    <asp:Panel ID="pnlAddEdit" runat="server" CssClass="modalPopup" style = "display:none">
    <asp:Label Font-Bold = "true" ID = "Label4" runat = "server" Text = "Nature Of Work Details" ></asp:Label>
    <br />
        <table align = "center">      
            <tr>
                <td>
                <asp:Label ID = "Label2" runat = "server" Text = "Nature Of Work Name" ></asp:Label>
                </td>
                <td>
                <asp:TextBox ID="txtNOWname" runat="server"></asp:TextBox>    
                </td>
            </tr>
            <tr>
                <td>
                    <asp:RadioButton id="RBEnable" Text="Enabled"  runat="server"/>
                    <br>
                    <asp:RadioButton id="RBDisable" Text="Disabled"  runat="server"/>
                    <br>
                </td>            
            </tr>
            <tr>
                <td>
                <asp:Button ID="btnSave" runat="server" Text="Save" OnClick = "Save" />
                </td>
                <td>
                <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick = "return Hidepopup()"/>
                </td>
            </tr>
        </table>
    </asp:Panel>
    <asp:LinkButton ID="lnkFake" runat="server"></asp:LinkButton>
        <cc1:ModalPopupExtender ID="popup" runat="server" DropShadow="false"
        PopupControlID="pnlAddEdit" TargetControlID = "lnkFake"
        BackgroundCssClass="modalBackground">
        </cc1:ModalPopupExtender>
</ContentTemplate> 
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID = "GridView1" />
    <asp:AsyncPostBackTrigger ControlID = "btnSave" />
    </Triggers> 

后面的代码是:-

private String strConnString = ConfigurationManager.ConnectionStrings["TempConnectionString"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.BindData();
        }
    }

    private void BindData()
    {
        string strQuery = "select NatureOfWorkID,NatureOfWorkName,IsNOWEnabled" +
                           " from NatureOfWork";
        SqlCommand cmd = new SqlCommand(strQuery);
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }

    private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
                return dt;
            }
        }
    }

    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        this.BindData();
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
4

5 回答 5

2

如果您的 B 逻辑增加,则声明一个公共方法并使用它类似于 inline if

例子

代码背后

public string  IsNowEnable(object data)
{
  return (int)data == 1 ? "Enabled" : "Disabled";
}

然后在 aspx

<asp:Label Text="<%# IsNowEnable(Eval("IsNOWEnabled"))%>" runat="server" />
于 2012-10-09T07:13:30.537 回答
1

使用RowDatabound事件和DataRowView属性如下...

void GridView_RowDataBound (Object sender, GridViewRowEventArgs e)
  {
    // Check for a row in edit mode.
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      DataRowView rowView = (DataRowView)e.Row.DataItem;
      e.Row.Cells[2].Text=rowView["IsNOWEnabled"].ToString()=="1"?"Enabled":"Disabled";
    }
}
于 2012-10-09T06:39:49.453 回答
0

为什么不使用 asp.net 标签而不是有界字段?然后您可以在GridView的ItemDataBound事件中更改asp.net标签的文本。

于 2012-10-09T06:36:12.353 回答
0

您可以使用内联 if 语句,例如:

<asp:Label Text="<%# Eval("IsNOWEnabled") == 1 ? "Enabled" : "Disabled" %>" runat="server" />
于 2012-10-09T06:40:50.360 回答
0

只需使用标记。你可以用另一个TemplateField来做这个。就像是:

<asp:TemplateField HeaderText="Enabled/Disabled" SortExpression="IsNOWEnabled">
    <ItemTemplate>
        <%# ((bool)Eval("IsNOWEnabled"))? "Enabled" : "Disabled" %>
    </ItemTemplate>
</asp:TemplateField>
于 2012-10-09T06:43:21.947 回答