我想隐藏我在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"> </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"> </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"> </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(),但它不起作用。它给我一般错误
如果记录中有空格或空格,请指导我如何隐藏我的电子邮件?我的代码中是否存在任何语法错误。?
答案 0 :(得分:2)
将风格应用于显示:无 Contact_Person &#39; tr&#39;动态如下所述:
<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>