如何使用CSS隐藏ASP:BoundField

时间:2019-03-18 01:06:37

标签: css asp.net

我想用CSS隐藏我的asp:BoundField,但是当我添加visible = false时,我无法访问这些值。所以我试图用css隐藏它,但是BoundFiled中不允许css,所以我尝试添加Updatepanel,div等,但是它们是不允许的。如何用CSS隐藏我的BoundField。

我想用CSS隐藏的代码

                <asp:BoundField DataField = "compras_id" HeaderText="Compras ID" HtmlEncode="false" />
                <asp:BoundField DataField = "numero_contrato" HeaderText = "Numero contrato" HtmlEncode="false" />
                <asp:BoundField DataField="marca" HeaderText="Marca" HtmlEncode="false"  />
                <asp:BoundField DataField="designacion" HeaderText="Designacion"  HtmlEncode="false"  />
                <asp:BoundField DataField="tipo" HeaderText="Tipo"  HtmlEncode="false"  />
                <asp:BoundField DataField="referencia" HeaderText="Referencia"  HtmlEncode="false"  />
                <asp:BoundField DataField="plazo" HeaderText="Plazo"  HtmlEncode="false" />
                <asp:BoundField DataField="nombre_proveedor" HeaderText="Proveedor"  HtmlEncode="false"  />
                <asp:BoundField DataField="cantidad_requerida" HeaderText="Cantidad requerida"  HtmlEncode="false" />
                <asp:BoundField DataField="cantidad_pedida" HeaderText="Cantidad pedida"  HtmlEncode="false"  />

我的CSS

.myHide
{
    display:none;
}

这就是为什么我需要隐藏BoundFields这是我的前端

<asp:GridView ID="gvPrimaryGrid" runat="server" AutoGenerateColumns="false" ShowFooter="true" onrowdatabound="gvPrimaryGrid_RowDataBound"
                ShowHeaderWhenEmpty="true" AllowPaging="True" OnPageIndexChanging="gridView_PageIndexChanging" DataKeyNames="compras_id"
                 OnRowCommand="gvPrimaryGrid_RowCommand"  OnRowEditing="gvPrimaryGrid_RowEditing" OnRowCancelingEdit="gvPrimaryGrid_RowCancelingEdit"
                OnRowUpdating="gvPrimaryGrid_RowUpdating"  CellPadding="3" AllowUserToResizeColumns="True" PageSize="5" AllowSorting="true" onsorting="gvPrimaryGrid_Sorting"  GridLines="None" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
                <PagerSettings  Mode="Numeric"  />         
                <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                       <%--<asp:CheckBox ID="CheckBox1" AutoPostBack="true" EnableViewState="true"  runat="server" onclick="Check_Click(this)" OnCheckedChanged="chkRow_CheckChanged" />--%>
                       <asp:CheckBox ID="chkRow" AutoPostBack="true"  runat="server" OnCheckedChanged="chkRow_CheckChanged" />
                           <%--<asp:CheckBox ID="chkRow" runat="server" />--%>
                        <asp:ImageButton ImageUrl="~/Images/edit.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px"/>
                        <asp:ImageButton ImageUrl="~/Images/delete.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px"/>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:ImageButton ImageUrl="~/Images/save.png" runat="server" CommandName="Update" ToolTip="Update" Width="20px" Height="20px"/>
                        <asp:ImageButton ImageUrl="~/Images/cancel.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px"/>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:ImageButton ImageUrl="~/Images/addnew.png" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px"/>
                    </FooterTemplate>
                </asp:TemplateField>   

                    <asp:BoundField DataField = "compras_id" HeaderText="Compras ID" HtmlEncode="false" />
                    <asp:BoundField DataField = "numero_contrato" HeaderText = "Numero contrato" HtmlEncode="false" />
                    <asp:BoundField DataField="marca" HeaderText="Marca" HtmlEncode="false"  />
                    <asp:BoundField DataField="designacion" HeaderText="Designacion"  HtmlEncode="false"  />
                    <asp:BoundField DataField="tipo" HeaderText="Tipo"  HtmlEncode="false"  />
                    <asp:BoundField DataField="referencia" HeaderText="Referencia"  HtmlEncode="false"  />
                    <asp:BoundField DataField="plazo" HeaderText="Plazo"  HtmlEncode="false" />
                    <asp:BoundField DataField="nombre_proveedor" HeaderText="Proveedor"  HtmlEncode="false"  />
                    <asp:BoundField DataField="cantidad_requerida" HeaderText="Cantidad requerida"  HtmlEncode="false" />
                    <asp:BoundField DataField="cantidad_pedida" HeaderText="Cantidad pedida"  HtmlEncode="false"  />

                 <asp:TemplateField HeaderText="Compras ID" SortExpression="compras_id" Visible="false">
                    <ItemTemplate>                       
                        <asp:Label DataField="compras_id" HtmlEncode="false"  ID="lblPurschaseID" CssClass="gridTextbox" Text='<%# Eval("compras_id") %>' runat="server" />
                    </ItemTemplate>
                    <%--<EditItemTemplate>
                        <asp:TextBox CssClass="gridTextbox" ID="txtType" Text='<%# Eval("tipo") %>' runat="server" />
                    </EditItemTemplate>--%>
                    <FooterTemplate>
                        <asp:TextBox  CssClass="gridTextbox" ID="txtPurschaseIDFooter" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>    
                  <asp:TemplateField HeaderText="Numero contrato" >
                    <ItemTemplate>                       
                        <asp:Label DataField="numero_contrato"  HtmlEncode="false"  ID="lblContractNumber" CssClass="gridTextbox" Text='<%# Eval("numero_contrato") %>' runat="server" />
                    </ItemTemplate>
                    <EditItemTemplate>   
                <asp:DropDownList  ID="drGridContractID" Runat="server"  AutoPostBack="false"  OnSelectedIndexChanged="DropDown_SelectedIndexChanged" />
                          <%-- <asp:TextBox CssClass="gridTextbox" ID="txtSupplier" Text='<%# Eval("nombre_proveedor") %>' runat="server" />--%>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox  CssClass="gridTextbox" ID="txtContractNumberFooter" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>    
                <asp:TemplateField HeaderText="Marca" SortExpression="marca">
                    <ItemTemplate>                       
                        <asp:Label DataField="Marca"  HtmlEncode="false"  ID="lblMarca" CssClass="gridTextbox" Text='<%# Eval("marca") %>' runat="server" />
                    </ItemTemplate>
                     <%-- <EditItemTemplate>
                        <asp:TextBox CssClass="gridTextbox" ID="txtBrand" Text='<%# Eval("marca") %>' runat="server" />
                    </EditItemTemplate>--%>
                    <FooterTemplate>
                        <asp:TextBox  CssClass="gridTextbox" ID="txtCountryFooter" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>

这是我的后端

private void GetSelectedRows()
    {
        DataTable dt = new DataTable();
        DataTable dt2 = new DataTable();
        if (ViewState["SelectedRows"] != null)
        {
            dt = (DataTable)ViewState["SelectedRows"];
        }
        else
        {
            dt = CreateDataTable();
        }
            //CheckBox chkAll = (CheckBox)gvPrimaryGrid.HeaderRow.Cells[0].FindControl("chkAll");
        for (int i = 0; i < gvPrimaryGrid.Rows.Count; i++)
        {
           /* if (chkAll.Checked)
            {
                dt = AddRow(gvPrimaryGrid.Rows[i], dt);
            }*/
            //else
            //{
                CheckBox chk = ((CheckBox)gvPrimaryGrid.Rows[i].Cells[0].FindControl("chkRow"));
                if (chk.Checked)
                {
                    dt2 = AddRow(gvPrimaryGrid.Rows[i], dt);
                }
                else

                {
                    dt2 = RemoveRow(gvPrimaryGrid.Rows[i], dt);
                }
           // }
        }
        ViewState["SelectedRows"] = dt2;
    }


    private DataTable CreateDataTable()
    {
        DataTable dt = new DataTable();
            dt.Columns.Add("compras_id");
            dt.Columns.Add("numero_contrato");
            dt.Columns.Add("marca");
            dt.Columns.Add("designacion");
            dt.Columns.Add("tipo");
            dt.Columns.Add("referencia");
            dt.Columns.Add("plazo");
            dt.Columns.Add("nombre_proveedor");
            //dt.Columns.Add("cantidad_requerida");
            dt.Columns.Add("cantidad_pedida");
            dt.Columns.Add("cantidad_entregada");
            dt.AcceptChanges();


        return dt;
    }

    private DataTable AddRow(GridViewRow gvRow, DataTable dt)
    {

        DataRow[] dr = dt.Select("compras_id = '" + gvRow.Cells[1].Text + "'");
        if (dr.Length <= 0)
        {
            int i = 1;
            dt.Rows.Add();
            dt.Rows[dt.Rows.Count - 1]["compras_id"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["numero_contrato"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["marca"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["designacion"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["tipo"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["referencia"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["plazo"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["nombre_proveedor"] = gvRow.Cells[i++].Text;
            //dt.Rows[dt.Rows.Count - 1]["cantidad_requerida"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["cantidad_pedida"] = gvRow.Cells[i++].Text;
            dt.Rows[dt.Rows.Count - 1]["cantidad_entregada"] = gvRow.Cells[i++].Text;
            dt.AcceptChanges();
        }
        return dt;
    }

    private DataTable RemoveRow(GridViewRow gvRow, DataTable dt)
    {
        DataRow[] dr = dt.Select("compras_id = '" + gvRow.Cells[1].Text + "'");
        if (dr.Length > 0)
        {
            dt.Rows.Remove(dr[0]);
            dt.AcceptChanges();
        }
        return dt;
    }

    private void BindSecondaryGrid()
    {
        DataTable dt = (DataTable)ViewState["SelectedRows"];
        gvSecondaryGrid.DataSource = dt;
        gvSecondaryGrid.DataBind();
    }

    protected void chkRow_CheckChanged(object sender, EventArgs e)
    {
        GetSelectedRows();
        SetData();
        BindSecondaryGrid();
    }

    private void SetData()
    {
        CheckBox chkAll = (CheckBox)gvPrimaryGrid.HeaderRow.Cells[0].FindControl("chkAll");
        //chkAll.Checked = true;
        if (ViewState["SelectedRows"] != null)
        {
            DataTable dt = (DataTable)ViewState["SelectedRows"];
            for (int i = 0; i < gvPrimaryGrid.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)gvPrimaryGrid.Rows[i].Cells[0].FindControl("chkRow");
                if (chk != null)
                {
                    DataRow[] dr = dt.Select("compras_id = '" + gvPrimaryGrid.Rows[i].Cells[1].Text + "'");
                    chk.Checked = dr.Length > 0;
                    //if (!chk.Checked)
                    //{
                    //    chkAll.Checked = false;
                    //}
                }
            }
        }
    }

如您所见,我使用会话存储表中的值,我创建了一个具有两列相同名称的表以进行编辑,并且还在用户可以添加的情况下在表与新表之间创建了链接通过选中第一个表的复选框来创建新行。

1 个答案:

答案 0 :(得分:0)

我会使用templatefield而不是boundfield。

别忘了,当您在Web控件上将可见性设置为false时,它们不会呈现给响应

<asp:templatefield>
   <itemtemplate>
      <span class='myhide'><%# eval("columnname") %></span>
   </itemtemplate>
</asp:templatefield>

此外,中继器可以使您对呈现的HTML进行精确控制,这可以为您提供更好的结果。