如果数据库值为 NULL,则在 ListView 中隐藏 Div

如何解决如果数据库值为 NULL,则在 ListView 中隐藏 Div

我有一个 ListView,它显示每个项目的不同类型的目录。如果“pdflabel”、“pdftechlabel”、“pdfecolabel”等的值为NULL,我想隐藏目录div...

我进行了大量搜索,但我有限的知识使我无法按预期连接所有点以实现此功能。非常感谢任何帮助。

<div id="catalog">
    <asp:ListView ID="ListViewCatalogs" runat="server" DataSourceID="SqlCatalogs" OnItemDataBound="ListViewCatalogs_ItemDataBound">
        <EmptyDataTemplate>
        </EmptyDataTemplate>
        <ItemTemplate>
            <div id="pdfcatalogs" runat="server">
                <div id="brochure" class="pdfcat" runat="server" >
                    <asp:HyperLink ID="HyperLink2" CssClass="pdflinks" runat="server" Target="_blank" NavigateUrl='<%# Eval("catlinkpath","Catalogs/{0}.pdf") %>' title='<%# Eval("pdfname") %>' ImageUrl='<%# Eval("pdficon") %>' />
                    <br />
                    <asp:Label ID="catnameLabel" CssClass="pdflinks" runat="server" Text='<%# Eval("pdflabel","{0} Brochure") %>' />
                </div><!--end brochure-->

                <div id="techbrochure" class="pdfcat" runat="server">
                    <asp:HyperLink ID="HyperLink6" CssClass="pdflinks" runat="server" Target="_blank" NavigateUrl='<%# Eval("techlinkpath","Catalogs/{0}.pdf") %>' title='<%# Eval("pdftechname") %>' ImageUrl='<%# Eval("pdftechicon") %>' />
                    <br />
                    <asp:Label ID="technameLabel" CssClass="pdflinks" runat="server" Text='<%# Eval("pdftechlabel") %>' />
                </div><!--end techbrochure-->

                <div id="ecobrochure" class="pdfcat" runat="server" >
                    <asp:HyperLink ID="HyperLink3" CssClass="pdflinks" runat="server" Target="_blank" NavigateUrl='<%# Eval("ecolinkpath","Catalogs/{0}.pdf") %>' title='<%# Eval("pdfeconame") %>' ImageUrl='<%# Eval("pdfecoicon") %>' />
                    <br />
                    <asp:Label ID="econameLabel" CssClass="pdflinks" runat="server" Text='<%# Eval("pdfecolabel") %>' />
                </div><!--end ecobrochure-->

                <div id="sdsbrochure" class="pdfcat" runat="server" >
                    <asp:HyperLink ID="HyperLink5" CssClass="pdflinks" runat="server" Target="_blank" NavigateUrl='<%# Eval("sdslinkpath","Catalogs/{0}.pdf") %>' title='<%# Eval("pdfeconame") %>' ImageUrl='<%# Eval("pdfsdsicon") %>' />
                    <br />
                    <asp:Label ID="sdsnameLabel" CssClass="pdflinks" runat="server" Text='<%# Eval("pdfsdslabel") %>' />
                </div><!--end sdsbrochure-->

                <div id="otherbrochure" class="pdfcat" runat="server" >
                    <asp:HyperLink ID="HyperLink7" CssClass="pdflinks" runat="server" Target="_blank" NavigateUrl='<%# Eval("otherlinkpath","Catalogs/{0}.pdf") %>' title='<%# Eval("pdfothername") %>' ImageUrl='<%# Eval("pdfothericon") %>' />
                    <br />
                    <asp:Label ID="othernameLabel" CssClass="pdflinks" runat="server" Text='<%# Eval("pdfotherlabel") %>' />
                </div><!--end otherbrochure-->
            </div><!--end pdfcatalogs-->
        </ItemTemplate>
        <LayoutTemplate>
            <div id="itemPlaceholderContainer" runat="server" border="0" style=""><span id="itemPlaceholder" runat="server"></span></div>
        </LayoutTemplate>
    </asp:ListView>
</div><!--end catalog-->

我根据 Albert D. Kallal 的原始答案和更新后的答案添加了以下内容(更改了 ListView 而不是 GridView)。

        private void AcceptData(string connectionString)
    {
        if (IsPostBack == false)
        {
            string strSQL;
            strSQL = "SELECT pdflabel,pdftechlabel,pdfecolabel,pdfsdslabel,pdfotherlabel from tblcatalogs";

            using (SqlCommand cmdSQL = new SqlCommand(strSQL,new SqlConnection(connectionString)))
            {
                cmdSQL.Connection.Open();
                ListViewCatalogs.DataSource = cmdSQL.ExecuteReader();
                ListViewCatalogs.DataBind();
            }
        }
    }

    protected void ListViewCatalogs_ItemDataBound(object sender,ListViewItemEventArgs e)
    {
        if (e.Item.GetType() == typeof(ListViewDataItem))
        {
            Label lTech = (Label)e.Item.FindControl("technameLabel");
            if (lTech.Text == "testing123")
            {
                HtmlGenericControl MyDiv = (HtmlGenericControl)e.Item.FindControl("techbrochure");
                MyDiv.Visible = false;
            }
        }
    }

然而,它仍然没有隐藏 div。我希望 div 实际上是“display:none”,这样它就不会占用页面中的空间。我目前的问题是我仍然有一个空 div 的空间。

我找到了一些内联选项,但这对我也不起作用。

<div id="techbrochure" class="pdfcat" runat="server" visible='<%# Eval("pdftechname") == null ? true:false %>'>
     <asp:HyperLink ID="HyperLink6" CssClass="pdflinks" runat="server" Target="_blank" NavigateUrl='<%# Eval("techlinkpath","Catalogs/{0}.pdf") %>' title='<%# Eval("pdftechname") %>' ImageUrl='<%# Eval("pdftechicon") %>' />
     <br />
     <asp:Label ID="technameLabel" CssClass="pdflinks" runat="server" Text='<%# Eval("pdftechlabel") %>' />
</div><!--end techbrochure-->

解决方法

这是个好问题!

你如何为一个 gridview、listview、formview 以及 asp.net 中的“任何”转发器对象执行此操作?它们的工作方式几乎完全相同。

因此,一般的方法是按照您的方式构建所有内容,并且有一个非常方便且不错的事件可以为这些所谓的“中继器”中的每一行触发。这称为 RowDataBound 事件。

它在数据绑定过程中为每一行触发,并让您放置漂亮的小例程来轻松完成任务(如您的问题)。所以这个事件会触发,并让您在填充数据后,但在最终渲染过程之前,将脏手放在行上。 (如前所述,您在列表视图中学习了这一点,然后几乎相同的方法适用于所有其他数据绑定控件 - (它们在给定的事件模型方面几乎完全相同)。

现在还有一个问题。我想我们可以使用 sql 来检查空值,而不是首先返回数据 - 对吗? (只是想建议 - 因为看起来 pdfcatalogs 是整行(是吗?)。所以理论上我们可以更改 sql 以防止这种情况发生。

甚至说,一旦我们拉出该数据表,我们就可以过滤数据以不包括这些行。但是,到目前为止还不清楚我们是基于此隐藏整行,还是仅隐藏该行的某些内容?所以不要排除使用过滤器 - 它们非常易于使用,因此您首先可以防止数据行成为数据表的一部分。

所以,在大多数情况下,为了隐藏这些行,我不会首先包含这些行。但是,我们经常仍然需要/想要条件格式。 (因此,如果您想根据其值添加/具有/更改某些行列的颜色,那么我们将要做的将并且可以很好地工作。

首先,你可以快速浏览一下我最近的这篇文章——它是一个网格,但正如我指出的,一个相同的方法适用于列表视图:

How to Highlight Cells with a certain value from the DB in a GridView in ASP .NET using VB Programming?

在上面,我想为行中的一些值着色。在这种情况下,我们只是要隐藏一些 - 但方法在执行上是相同的。

所以,在我们的 RowDataBound 事件中,我们简单地说检查 catnamelabel,然后隐藏 div

protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
    // this is a data row!

    Label catlbel = e.Row.FindControl("catnameLabel");
    if (catlbel.Text == "")
    {
        HtmlGenericControl MyDiv = e.Row.FindControl("pdfcatalogs");
        MyDiv.Style("Display") = "none";
    }
}

因此您可以添加尽可能多的测试。并且空值将成为标签中的“”

编辑:--------------

这是使用列表视图的示例。

首先,标记 - 我们有一些标准列 - 并注意我放置在最后两列周围的 div。

所以,我们有这个:

  <asp:ListView ID="ListView1" runat="server" style="width:700px">
        <ItemTemplate>
            <tr style="">
                <td><asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' /></td>
                <td><asp:Label ID="HotelNameLabel" runat="server" Text='<%# Eval("HotelName") %>' /></td>
                <td><asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>' /></td>
                <td><asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' /></td>

                <div id="ProvinceAndComments" runat="server">

                    <td><asp:Label ID="ProvinceLabel" runat="server" Text='<%# Eval("Province") %>' /></td>
                    <td><asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' /></td>

                </div>
            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            <table id="itemPlaceholderContainer" runat="server" border="0" style="">
                <tr runat="server" style="">
                    <th runat="server">ID</th>
                    <th runat="server" width="140px">HotelName</th>
                    <th runat="server">FirstName</th>
                    <th runat="server">City</th>
                    <th runat="server">Province</th>
                    <th runat="server">Description</th>
                </tr>
                <tr id="itemPlaceholder" runat="server">
                </tr>
            </table>
        </LayoutTemplate>
    </asp:ListView>

(我什至让向导为我创建行 - 毕竟这只是问答,所以工作量不大。

好的,现在在我们的 on-load 事件中,我们使用以下代码加载列表视图:

protected void Page_Load(object sender,System.EventArgs e)
{
if (IsPostBack == false)
   {
    string strSQL;
    strSQL = "SELECT ID,HotelName,FirstName,City,Province,Description from tblHotels";

    using (SqlCommand cmdSQL = new SqlCommand(strSQL,new SqlConnection(My.Settings.Test3)))
    {
        cmdSQL.Connection.Open();
        ListView1.DataSource = cmdSQL.ExecuteReader;
        ListView1.DataBind();
    }
   }
}

好的,上面是几行代码。我们现在得到/看到这个:

enter image description here

现在,注意上面标记中的“div”。这个:

<div id="ProvinceAndComments" runat="server">

   <td><asp:Label ID="ProvinceLabel" runat="server" Text='<%# Eval("Province") %>' /></td>
   <td><asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' /></td>

</div>

好的,让我们“隐藏”这个 div 说 if Province = "AB"。

所以,在物品绑定事件中。 (显示属性表 - 只需双击该事件 - 您就会跳转到代码编辑器。

所以,我们现在有这么一小段代码:

protected void ListView1_ItemDataBound(object sender,ListViewItemEventArgs e)
{
    if (e.Item.GetType == typeof(ListViewDataItem))
    {
       Label lProvince = e.Item.FindControl("ProvinceLabel");
       if (lProvince.Text == "AB")
          {
          HtmlGenericControl MyDiv = e.Item.FindControl("ProvinceAndComments");
          MyDiv.Visible = false;
          }
    }
}

好的,所以我们只是“隐藏”那个 div。它围绕着最后两列。所以,现在运行这段代码,你会得到:

enter image description here

如前所述,我可以根据我想要的任何标准隐藏整行,但上面的“div”围绕着最后两列。请注意,您必须给它一个“ID”,当然还要使用 runat="server" 作为后面的代码才能查看 + 使用此类控件。现在我在列和控件周围使用了一个 div。 我想您可以按名称取出控件,然后以这种方式隐藏它们,但我取决于您想要隐藏或显示的程度。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-