如何在gridview项目模板中隐藏trtd

本文关键字:隐藏 trtd 项目 gridview | 更新日期: 2023-09-27 17:53:37

我想隐藏我在gridview itemtemplate中使用的tr和td,基于如果字段为空或有任何空白的条件。

这是我的gridview标记:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    CellPadding="4" EnableModelValidation="True" ForeColor="#333333" 
    GridLines="Horizontal" Width="1000px" onrowcreated="GridView1_RowCreated" DataKeyNames="id">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:TemplateField HeaderText="Directory">
        <ItemTemplate>
        <table width="1000px">
        <tr><td colspan="4">&nbsp;</td></tr>
        </table>
        <table width="1000px">
        <tr class="border_bottom"><td align="center" colspan="4" style="color: #5D7B9D;"><strong><%# HighlightText(Eval("name").ToString()) %></strong></td></tr>
        <tr class="border_bottom" style='<%#Eval("contact_person") == ""? "display:none": "display:block"%>'><td align="center" colspan="4" style="color:Black"><%#Eval("contact_person")%></td></tr>
        <tr style="width:1000px;"><td colspan="2">&nbsp;</td></tr>
        <tr><td style="width:auto;">Address: <%#Eval("address")%></td><td style="width:auto;">Country: <%# HighlightText(Eval("country").ToString())%></td><td style='<%#Eval("city") == ""? "display:none": "display:block"%>'>City: <%# HighlightText(Eval("city").ToString())%></td><td style='<%#Eval("zip") == ""? "display:none":"display:block" %>'>Zip: <%#Eval("zip")%></td></tr>
        <tr><td style="width:auto;">Phone: <%#Eval("phone")%></td><td style='<%#Eval("mobile") == ""? "display:none": "display:block"%>'>Mobile: <%#Eval("mobile")%></td><td style="width:auto;">Fax: <%#Eval("fax")%></td><td style="width:auto;color: #0000FF;"><p style='<%# string.IsNullOrWhiteSpace(Eval("email").ToString().Trim())? "display:none": "display:block"%>'><span style="color:Black;">Email:</span> <%#Eval("email")%></p><br /><asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click" 
                Text='<%# Eval("website") %>' ForeColor="#DD0303"></asp:LinkButton></td></tr>
                <tr><td style="width:auto;color:Black;">Product Category: <%# HighlightText(Eval("product_category").ToString())%></td><td style="width:auto;color:Black;">Activity: <%# HighlightText(Eval("activity").ToString())%></td><td colspan="2" style="width:auto;color:Black;">Description: <%#Eval("Description")%></td></tr>
        </table>
        <table width="1000px">
        <tr><td colspan="4">&nbsp;</td></tr>
        </table>
        </ItemTemplate>
            <HeaderStyle Font-Bold="True" Font-Size="X-Large" />
        </asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>

我通过这个类绑定gridview -

 public void bind()
    {
        try
        {
            tot_record = g1.retrieve_val("select count(*) from tblDirectory where id is  not null " + Session["Country"] + Session["Product"] + Session["Activity"] + Session["Name"] + "");
            dt = g1.return_dt("select  * from tblDirectory where id is  not null  " + Session["Country"] + Session["Product"] + Session["Activity"] + Session["Name"] + Session["Check"] + " order by name");
            if (dt.Rows.Count > 0)
            {
                adsource = new PagedDataSource();
                adsource.DataSource = dt.DefaultView;
                adsource.PageSize = 10;
                adsource.AllowPaging = true;
                adsource.CurrentPageIndex = pos;
                btnfirst.Enabled = !adsource.IsFirstPage;
                btnprevious.Enabled = !adsource.IsFirstPage;
                btnlast.Enabled = !adsource.IsLastPage;
                btnnext.Enabled = !adsource.IsLastPage;
                GridView1.DataSource = adsource;
                GridView1.DataBind();
                s = Convert.ToInt32(this.ViewState["vs"].ToString());
            }
            else
            {
                GridView1.DataSource = null;
                GridView1.DataBind();
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }

当我使用<tr style='<%#Eval("contact_person") == ""? "display:none": "display:block"%>'><td align="center" colspan="4" style="color:Black"><%#Eval("contact_person")%></td></tr>时我能够隐藏tr,但是,在一些数据中,空白空间存在于数据库中,它不隐藏。我还在电子邮件中使用了IsNullOrWhitespace(),但它不起作用。它给了我一般的错误。
请指导我如何隐藏我的电子邮件,如果空白或空白的记录存在?是否有任何类型的语法错误,我在我的代码做。

如何在gridview项目模板中隐藏trtd

Contact_Person 'tr'动态地为display:none应用样式 如下所述:

<tr class="border_bottom" style='<%#Eval("contact_person") == ""? "display:none": "display:block"%>'>
    <td align="center" colspan="4" style="color: Black"><%#Eval("contact_person")%></td>
</tr>

更新:

如果你想在电子邮件中做同样的事情,那么你可以像下面提到的那样使用它:

<tr>
    <td style="width: auto;">Phone: <%#Eval("phone")%></td>
    <td style="width: auto;">Mobile: <%#Eval("mobile")%></td>
    <td style="width: auto;">Fax: <%#Eval("fax")%></td>
    <td style="width: auto; color: #0000FF;">
        <p style='<%# string.IsNullOrWhiteSpace(Eval("email").ToString().Trim())? "display:none": "display:block"%>'>
            <span style="color: Black;">Email:</span> <%#Eval("email")%>
        </p>
        <br />
        <asp:LinkButton ID="LinkButton2" runat="server" OnClick="LinkButton2_Click"
            Text='<%# Eval("website") %>' ForeColor="#DD0303"></asp:LinkButton>
    </td>
</tr>