如何隐藏单个formview行

时间:2013-04-05 17:08:25

标签: c# asp.net formview

我试图根据会话变量的当前状态隐藏单行的formview。

目前我试图在更新面板中包含该行,然后将Visible属性设置为true或false。

这目前无效。

以下是代码:

 <asp:FormView ID="FormView1" runat="server" Height="51px" Width="387px">
         <EditItemTemplate>
           coffeeName:
           <asp:TextBox ID="coffeeNameTextBox" runat="server" 
               Text='<%# Bind("coffeeName") %>' />
           <br />
           coffeeOrigin:
           <asp:TextBox ID="coffeeOriginTextBox" runat="server" 
               Text='<%# Bind("coffeeOrigin") %>' />
           <br />
           coffeeStrength:
           <asp:TextBox ID="coffeeStrengthTextBox" runat="server" 
               Text='<%# Bind("coffeeStrength") %>' />
           <br />
           coffeePrice:
           <asp:TextBox ID="coffeePriceTextBox" runat="server" 
               Text='<%# Bind("coffeePrice") %>' />
           <br />
           coffeeGrind:
           <asp:TextBox ID="coffeeGrindTextBox" runat="server" 
               Text='<%# Bind("coffeeGrind") %>' />
           <br />
           coffeeQty:
           <asp:TextBox ID="coffeeQtyTextBox" runat="server" 
               Text='<%# Bind("coffeeQty") %>' />
           <br />
           coffeeRRP:
           <asp:TextBox ID="coffeeRRPTextBox" runat="server" 
               Text='<%# Bind("coffeeRRP") %>' />
           <br />
           <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
               CommandName="Update" Text="Update" />
           &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
               CausesValidation="False" CommandName="Cancel" Text="Cancel" />
       </EditItemTemplate>



       <ItemTemplate>

           <b>Origin:</b>
           <asp:Label ID="coffeeOriginLabel" runat="server" 
               Text='<%# Bind("coffeeOrigin") %>' />
           <br />
                        <br />
            <b>Grind:</b>
           <asp:Label ID="coffeeGrindLabel" runat="server" 
               Text='<%# Bind("coffeeGrind") %>' />
           <br />
                        <br />
           <b>Strength:</b>
           <asp:Label ID="coffeeStrengthLabel" runat="server" 
               Text='<%# Bind("coffeeStrength") %>' />
           <br />
                        <br />
           <b>Price: £</b>
           <asp:Label ID="coffeePriceLabel" runat="server" 
               Text='<%# Bind("coffeePrice") %>' />
           <br />
         <br />

            ********** Panel used to enclose the stock level**********

           <asp:Panel ID="panelLevel" runat="server">
           <b>Stock Level:</b>

           <asp:Label ID="coffeeQtyLabel" runat="server" Text='<%# Bind("coffeeQty") %>' />
           <br />
                        <br />
                                     <br />
       </asp:Panel>

       </ItemTemplate>

   </asp:FormView>

我想要实现的目标。

目前的形式观点:

enter image description here

将面板可见性设置为false应产生以下输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

你能提供更多代码吗?更新面板在哪里?您如何设置行可见性?

试试这个,为我工作:

代码CS: 的

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

    private void BindFormView()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("coffeeName", typeof(string));
        dt.Columns.Add("coffeeOrigin", typeof(string));
        dt.Columns.Add("coffeeStrength", typeof(int));
        dt.Columns.Add("coffeePrice", typeof(double));
        dt.Columns.Add("coffeeGrind", typeof(string));
        dt.Columns.Add("coffeeQty", typeof(int));
        dt.Columns.Add("coffeeRRP", typeof(int));

        DataRow dr = dt.NewRow();
        dr["coffeeName"] = "Kenya";
        dr["coffeeOrigin"] = "Smooth";
        dr["coffeeStrength"] = 6;
        dr["coffeePrice"] = 2.99;
        dr["coffeeGrind"] = "Kenya";
        dr["coffeeQty"] = 6;

        dt.Rows.Add(dr);

        FormView1.DataSource = dt;
        FormView1.DataBind();
    }

    protected void btnHideRow_Click(object sender, EventArgs e)
    {
        Session["HideRow"] = Session["HideRow"] == null ? false :                                        (bool)Session["HideRow"];
        BindFormView();
    }

    protected void FormView1_DataBound(object sender, EventArgs e)
    {
        if (Session["HideRow"] != null)
        {
            var pnlToHide = FormView1.FindControl("panelLevel");
            pnlToHide.Visible = (bool)Session["HideRow"];
        }
    }