I want to hide tr and td I have used inside my gridview itemtemplate based on condition that if the field is empty or having any white spaces.
This is my gridview markup:
<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>
And I am binding my gridview through this class-
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();
}
}
When I am using <tr style='<%#Eval("contact_person") == ""? "display:none": "display:block"%>'><td align="center" colspan="4" style="color:Black"><%#Eval("contact_person")%></td></tr>
I am able to hide tr but, in some data where white space is present in database it is not hiding . I have also used IsNullOrWhitespace() in Email, but it is not working. It is giving me generic error.
Please guide me how can I hide my Email if empty or white space is present in record?Is there any kind of syntactical error I am doing in my code.?
Like this-
Email: <%#Eval("email")%>
– Omi Mar 26 '14 at 08:10tag, but it is still displaying Email text.
I have replace my code with this, but even if email is not present in the table it is still displaying Email text. – Omi Mar 26 '14 at 08:36''>
` – Omi Mar 26 '14 at 09:45