[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1239
  • Last Modified:

Hide Label control asp.net page using Javascript

I have a string bulider strText and I assign following Javascript code to show a label lblhidden which is hidden by default

strText.Append(" <a id='displayText' href='javascript:doShow(" + lblhidden.ClientID + ");'> ...View More</a> ");

This is my javascript,


<script type='text/javascript'>
       
    function doShow(id)
    {
        alert(id);
        document.all(id).style.display = "block";
    }
       
</script>

I am getting error saying invalid argument, I do hover the link and it does have right clientID.
Please help what i am doing wrong
0
Guru Ji
Asked:
Guru Ji
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
fritzfrancisCommented:
Have you tried the "visibility" property?
0
 
Guru JiAuthor Commented:
If I put a alert in my javascript with the id, I get

[object HTMLSpanElement] in the message box.
0
 
Guru JiAuthor Commented:
fritzfrancis, can you show me how to use the visibility property ?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
informaniacCommented:
Can't u use document.getElementById(id).style.display = "block";
0
 
Guru JiAuthor Commented:
Hey informaniac

I tried yours but now I get the following error

Object Required

and when I put the alert(id); in my Javascript code I get the message
[object HTMLSpanElement]
0
 
Rajar AhmedConsultantCommented:
try like this ?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="labelShow.aspx.cs" Inherits="CS_labelShow" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
        <script>
            function doShow(id) {
                alert(id);
                document.all(id).style.display = "block";
            }


</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label id="lblhidden" Text="text" runat="server" />
    </div>
    </form>
</body>
</html>

   protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder strText = new StringBuilder();        
        strText.Append("<a id=\"displayText\" href=\"javascript:doShow('" + lblhidden.ClientID + "');\"> ...View More</a>");
        Response.Write(strText);
    }

Open in new window

0
 
Guru JiAuthor Commented:
Hi meeran03,

I implemented your code and now I am getting correct id in Javascript but the only difference is in my aspx code my label is visible=false

<asp:Label id="lblhidden" Text="text" visible="false" runat="server" />

and when I do view source I cannot find my label id, so javascript gives me error saying Object Required.

How can I find that invisible label to make it visible ?
0
 
informaniacCommented:
Is your label like this?

<asp:Label id="lblhidden" runat="server" Visible="false" />

Do not set it's Visibility in markup.

If you set it's visibility it will not get rendered in the page and hence it will give that error of Object required.
0
 
Rajar AhmedConsultantCommented:
seting visible false doesnot render any control on view source . Alternatively, Please set style="display:none" and Remove visible false from your label control .
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now