Results 1 to 5 of 5

Thread: Making Label control visible when clicked on a button

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    211

    Making Label control visible when clicked on a button

    Hello folks,

    I have Label control that I made invisible in the Page_Load event of the .aspx page.

    When I click on a button, the label control should become visible. As expected, it becomes visible but disappears in a second....don't know why. Any help is greatly appreciated.

    Code:
    <&#37;@ Page Title="" Language="C#" MasterPageFile="~/SiteMasterPage.master" AutoEventWireup="true" CodeFile="DynamicTextboxControls.aspx.cs" Inherits="DynamicTextboxControls" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
        </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <h3>Dynamic Textbox Controls</h3>
    <fieldset style="width: 300px">
            <legend>From</legend>
            
                <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                    <asp:ListItem>Database</asp:ListItem>  
                    <asp:ListItem>Interactive</asp:ListItem>  
                </asp:RadioButtonList>            
                
        </fieldset>
         
         
            <table border="1">
                <tr> 
                    <td class="style1"><asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Validate();"/>
                    </td>
                </tr>
            </table>
         
         <div style="height: 149px">
         <table border="1">
                <tr> 
                    <td class="style1">
                        <asp:Label runat="server" id="Label1">visible </asp:Label>
    
                    </td>
                </tr>
            </table>
         
         </div>    
        <script type="text/javascript" language="javascript">
            function Validate() {
    
                var varLabel = document.getElementById("<%=Label1.ClientID%>");
    
                
                varLabel.style.display = "block";
                varLabel.style.visibility = "visible";        }
        </script>
    </asp:Content>
    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
    
            Label1.Style.Add("visibility", "hidden");
        }

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    211

    Re: Making Label control visible when clicked on a button

    This is what I found so far.

    If I use a HTML button control intead of using button asp control, it works fine (meaning the label control doesn't disappear). But if I use asp button control, the momement I click on button control, the label appears and it disappears...looks like I should somehow tell the asp button control not to invoke the server side event of that control...

    any suggestions on how to do this?

  3. #3
    Hyperactive Member jasonwucinski's Avatar
    Join Date
    Mar 2010
    Location
    Pittsburgh
    Posts
    452

    Re: Making Label control visible when clicked on a button

    did you want to do this on the client or server side? Client side means the page will not have to reload. if it's server sided, the page will post back and you can do it there.

    client side: uses javascript. add a client event handler to your asp button (or just use a regular html button).
    Code:
    javascript Code:
    1. <script type="text/javascript">
    2. function hideMe(){
    3. var getLabel  = document.getElementById("<%=Label1.ClientId %>")
    4. getLabel.style.visibility="hidden"
    5.  
    6. }
    7. </script>

    then just add your button, with client side event:

    asp.net Code:
    1. <asp:Button OnClientClick="hideMe()" runat="server" />

    if you want to do it on the post back, goto the code bedind:
    vb.net Code:
    1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.   if(isPostBack) then
    3.         Dim label As Label
    4.         label = Me.Label1
    5.         label.Visible = False
    6.  
    7.  
    8. end if    
    9.  
    10. dim getLabel
    11. end sub


    if you still have problems, let me know. i just free handed the code, so i'm sure there are typos.
    if i was able to help, rate my post!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    211

    Re: Making Label control visible when clicked on a button

    Hi,

    Thanks for responding. If you see the code I posted, I have done already all that stuff.

    I want client-side coding and not server side. when button is clicked, the label should become visible. If I use HTML button instead of asp button, the code works fine. But when I use ASP button control, when the button is clicked, the label control appears for a second and dis-appears again. The cause probably is: since I used asp button control, it posts back to server and the page gets loaded again. In the page_load event, the following code gets executed which makes the label control disappear.

    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
    
            Label1.Style.Add("visibility", "hidden");
        }

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Making Label control visible when clicked on a button

    Just check if you are in a postback or not
    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Label1.Style.Add("visibility", "hidden");
            }
        }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width