We help IT Professionals succeed at work.

How to toggle a <hr> tag in code behind(.aspx.cs page)?

viola123
viola123 asked
on
Hi All,
i have a gridView and have the following requirements:
<hr> is hidden when first loading the page;
when there is data in gridView---> display <hr>;
if there is no data in gridView---> hide <hr>;

i am using javascript to do it but i found <hr> is always hidden, can anyone point me to the right direction?

attached the code.


<hr id="hrExport" style=" display:none;" />

*********************************
string javaScript = string.Empty; 

        
        if (dt.Rows.Count > 0)
        {

            javaScript = @"<script type='text/javascript' >
                                function DisplayHr1() {
                                    var hr = document.getElementById('hrExport');
                                    hr.style.display = 'block'; 

                                }</script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "DisplayHr1", javaScript); 


        }
        else
        {
            javaScript = @"<script type='text/javascript' >
                                function DisplayHr2() {
                                    var hr = document.getElementById('hrExport');
                                    hr.style.display = 'none'; 

                                }</script>";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "DisplayHr2", javaScript); 

        }

Open in new window

Comment
Watch Question

Craig WagnerSoftware Architect
CERTIFIED EXPERT
Commented:
In this situation I think a simpler solution than JavaScript is in order.

In the markup use a Placeholder control.

<asp:Placeholder Id="hrPlaceholder" runat="server" />

Then in the codebehind just inject the right HTML.


if (dt.Rows.Count > 0)
{
    hrPlaceholder.Text = "<hr style=\"display: block\">";
}

Open in new window

Author

Commented:
CraigWagner:
thanks a lot. i will try and let you know the result

Author

Commented:
hi,
i got error:
'System.Web.UI.WebControls.PlaceHolder' does not contain a definition for 'Text'

thanks
Commented:
hi,
i figure it out:

if (dt.Rows.Count > 0)
        {

            string strHtml = "<hr style=\"display: block\">";
            hrPlaceholder.Controls.Add(new LiteralControl(strHtml));



        }
        else
        {

            hrPlaceholder.Controls.Clear();
}

Open in new window

Author

Commented:
i figure it out based on Craig's suggestion.

thank you Craig!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.