1

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">&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>

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.?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Omi
  • 427
  • 7
  • 21
  • 42

1 Answers1

2

Apply style to display:none for the Contact_Person 'tr' dynamically as mentioned below :

<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>

Update :

If you want to do same thing for email then you can use it as mentioned below :

<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>
SpiderCode
  • 10,062
  • 2
  • 22
  • 42
  • Hello, Thanks a lot for your suggestion, it worked :-) But I have one more doubt. If you see my code I have email and website both in a same td. Now if I want to check if email is empty or not, and if it is then I will not display it. I want to know how I can use the same code as mentioned by you in side

    Like this-

    Email: <%#Eval("email")%>

    – Omi Mar 26 '14 at 08:10
  • Hello I have tried using the same code inside

    tag, but it is still displaying Email text.

    Email: <%#Eval("email")%>

    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
  • string.IsNullOrWhiteSpace is not working. It is giving me Generic Error. Though style='<%#Eval("city") == ""? "display:none": "display:block"%>' This format is working fine, but in some data there are white spaces in my table I guess, that is why in some rows Email text is reflected. – Omi Mar 26 '14 at 09:40
  • Yes of course I will mark it as answer. I just need a final solution for this problem friend, that is why I am waiting. Though the previous suggestion of yours was very helpful. But I also want to check if there is any white spaces in my table. – Omi Mar 26 '14 at 09:42
  • you can use **Trim()** as well – SpiderCode Mar 26 '14 at 09:43
  • Like this `

    ''>

    `
    – Omi Mar 26 '14 at 09:45
  • see my updated answer for email **.ToString().Trim()** – SpiderCode Mar 26 '14 at 09:51
  • Still its not working :-( I am not understanding why the format is not working as suggested by you.I have also checked on this website and similar solution was suggested by some developer, http://stackoverflow.com/questions/11041488/how-to-conditionally-show-hide-link-in-detailsview-itemtemplate – Omi Mar 26 '14 at 10:18
  • Is there any problem if I use this format inside my Gridview Itemtemplate?? I tried doing same as suggested by you in your post, and gain I am experiencing a generic error. I guess there is some syntax problem. Please guide me friend. – Omi Mar 26 '14 at 10:21
  • Hello friend, I have edited my question and updated it with the present code. Please check and guide me where I am doing wrong? – Omi Mar 27 '14 at 05:34
  • I have updated my question again please check. I am still encountering same problem. I guess string.IsNullOrWhiteSpace() is not supporting within style. – Omi Mar 27 '14 at 12:36
  • No I am getting Generic error. I get this error whenever there is a syntax error on my page. – Omi Mar 27 '14 at 12:59
  • Helped me a lot thx. Also u could add (OR) in the condition like: style='<%#Eval("contact_person") == ""|| Eval("contact_person") == "ABC" ? "display:none": "display:block"%>' – Sam Apr 11 '17 at 13:53