2025年2月13日 星期四

[研究][ASP.NET]DetailsView 如何動態隱藏 BoundField 或 TemplateField 欄位不顯示?

[研究][ASP.NET]DetailsView 如何動態隱藏 BoundField 或 TemplateField 欄位不顯示?

2025-02-12

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

(舊文翻出,不太記得是否測過)

********************************************************************************

(下圖)沒做任何隱藏前



Default.aspx:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />&nbsp;
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" style="height: 21px" />
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>" 
            DeleteCommand="DELETE FROM [Table4] WHERE [SN] = @SN" 
            InsertCommand="INSERT INTO [Table4] ([BookName], [BookIntro], [BookPrice]) VALUES (@BookName, @BookIntro, @BookPrice)" 
            SelectCommand="SELECT * FROM [Table4]" 
            UpdateCommand="UPDATE [Table4] SET [BookName] = @BookName, [BookIntro] = @BookIntro, [BookPrice] = @BookPrice WHERE [SN] = @SN">
            <DeleteParameters>
                <asp:Parameter Name="SN" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="BookName" Type="String" />
                <asp:Parameter Name="BookIntro" Type="String" />
                <asp:Parameter Name="BookPrice" Type="Int32" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="BookName" Type="String" />
                <asp:Parameter Name="BookIntro" Type="String" />
                <asp:Parameter Name="BookPrice" Type="Int32" />
                <asp:Parameter Name="SN" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AllowPaging="True" 
             DefaultMode="Insert"
            AutoGenerateRows="False" DataKeyNames="SN" DataSourceID="SqlDataSource1" OnDataBound="DetailsView1_DataBound">
            <Fields>
                <asp:BoundField DataField="SN" HeaderText="SN" InsertVisible="False" ReadOnly="True" SortExpression="SN" />
                <asp:BoundField DataField="BookName" HeaderText="書名" SortExpression="BookName" />
                <asp:BoundField DataField="BookIntro" HeaderText="簡介" SortExpression="BookIntro" />
                <asp:TemplateField HeaderText="價格" SortExpression="BookPrice">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("BookPrice") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("BookPrice") %>'></asp:TextBox>
                        <asp:Label ID="Label_BookPrice" runat="server">註解</asp:Label>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("BookPrice") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
    </form>
</body>
</html>

Default.aspx.cs:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        // 使用欄位索引編號來隱藏,程式較簡潔,但欄位增減後要改索引麻煩
        protected void Button1_Click(object sender, EventArgs e)
        {
            //DetailsView1.Fields[0].Visible = false; // SN
            //DetailsView1.Fields[1].Visible = false; // BookName
            DetailsView1.Fields[2].Visible = false; // BoundField,BookIntro
            DetailsView1.Fields[3].Visible = false; // TemplateField,BookPrice
        }

        // BoundField 使用欄位資料名稱 (DataField) 來隱藏欄位
        // TemplateField 使用欄位顯示名稱 (HeaderText) 來隱藏欄位
        // 程式較麻煩,欄位增減維護較方便
        protected void Button2_Click(object sender, EventArgs e)
        {
            foreach (DataControlField field in DetailsView1.Fields)
            {
                if (field is BoundField)
                {
                    BoundField boundField = field as BoundField;
                    if (boundField.DataField == "BookIntro")
                    {
                        boundField.Visible = false;
                    }
                }
                else if (field is TemplateField)
                {
                    TemplateField templateField = field as TemplateField;
                    if (templateField.HeaderText == "價格")
                    {
                        templateField.Visible = false;
                    }
                }
            }
            DetailsView1.DataBind();
        }

        // 直接寫在 DataBound() 的方法
        protected void DetailsView1_DataBound(object sender, EventArgs e)
        {

            //error CS0030: 無法將類型 'System.Web.UI.Control' 轉換成 'System.Web.UI.WebControls.BoundField'
            //BoundField bf = (BoundField)DetailsView1.FindControl("BookIntro");
            BoundField bf = DetailsView1.Fields.OfType<BoundField>().FirstOrDefault(f => f.DataField == "BookIntro");

            //error CS0030: 無法將類型 'System.Web.UI.Control' 轉換成 'System.Web.UI.WebControls.TemplateField'
            //TemplateField tf = (TemplateField)DetailsView1.FindControl("BookPrice");
            TemplateField tf = (TemplateField)DetailsView1.Fields.Cast<DataControlField>().FirstOrDefault(f => f.HeaderText == "價格");

            // 根據控制項的型別設定 Visible 屬性
            if (bf != null)
            {
                bf.Visible = false;
            }
            if (tf != null)
            {
                tf.Visible = false;
            }
        }
        //---
        // 進階到控制 Cell
        /*
        protected void DetailsView1_DataBound(object sender, EventArgs e)
        {
            // 尋找欄位控制項
            BoundField field = DetailsView1.Fields[2] as BoundField;
            if (field == null)
            {
                TemplateField tempField = DetailsView1.Fields[2] as TemplateField;
                if (tempField != null)
                {
                    // 如果是 TemplateField,尋找其內部的控制項
                    Label label = DetailsView1.FindControl("簡介") as Label;
                    if (label != null)
                    {
                        // 隱藏控制項
                        label.Visible = false;
                    }
                }
            }
            else
            {
                // 如果是 BoundField,尋找其對應的 TableCell

                TableCell cell0 = DetailsView1.Rows[2].Cells[0]; //欄位名稱,一但隱藏,欄位值會偏移到欄位名稱位置
                TableCell cell1 = DetailsView1.Rows[2].Cells[1]; //欄位值
                if (cell0 != null)
                {
                    // 隱藏 TableCell
                    cell1.Visible = false;
                }
            }
            //---
            TemplateField tempField3 = DetailsView1.Fields[3] as TemplateField;
            if (tempField3 != null)
            {
                // 如果是 TemplateField,尋找其內部的控制項
                Label label = (Label)DetailsView1.FindControl("Label_BookPrice");
                if (label != null)
                {
                    // 隱藏控制項
                    label.Visible = false;
                }
            }
        }
        */
        //---
    }
}

(完)

相關

沒有留言:

張貼留言