How to make a Label control appear on a click of a buttonhttp://forums.asp.net/t/1809090.aspx/1?How+to+make+a+Label+control+appear+on+a+click+of+a+buttonThu, 31 May 2012 15:37:15 -040018090905004508http://forums.asp.net/p/1809090/5004508.aspx/1?How+to+make+a+Label+control+appear+on+a+click+of+a+buttonHow to make a Label control appear on a click of a button <p>Hello,</p> <p>I have a Label control and a button control. Both are asp control (not HTML).&nbsp; When clicked on button control the label should appear.</p> <p>Following is the code I wrote. And I want only client-side scripting. The problem is, the label control appears for a second and disappears right away when button is clicked. In the page load event, I put the code to make the Label control invisible. And in the property of the label control (at design time), it is set to visible. If I use a HTML button control, it works fine. Any help is greatly appreciated.</p> <pre class="prettyprint">protected void Page_Load(object sender, EventArgs e) { Label1.Style.Add(&quot;visibility&quot;, &quot;hidden&quot;); } &lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;Javascript_MakeaControlAppear.aspx.cs&quot; Inherits=&quot;FHLBSF.QRMDMS.WebUI.Javascript_MakeaControlAppear&quot; %&gt; &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt; &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt; &lt;head runat=&quot;server&quot;&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt; &lt;div&gt; &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&quot;Label&quot;&gt;Hello&lt;/asp:Label&gt; &lt;/div&gt; &lt;div&gt; &lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Button&quot; onclientclick=&quot;MakeLabelVisible();&quot;/&gt; &lt;/div&gt; &lt;/form&gt; &lt;script language=&quot;javascript&quot; type=&quot;text/javascript&quot;&gt; function MakeLabelVisible() { var labelcontrol = document.getElementById(&quot;&lt;%=Label1.ClientID%&gt;&quot;); labelcontrol.style.display = &quot;block&quot;; labelcontrol.style.visibility = &quot;visible&quot;; } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p></p> 2012-05-30T15:58:21-04:005004568http://forums.asp.net/p/1809090/5004568.aspx/1?Re+How+to+make+a+Label+control+appear+on+a+click+of+a+buttonRe: How to make a Label control appear on a click of a button <p>When you use an html control, it is client-side. When you use an asp.net control, it's server-side. You'll notice the runat=server attribute.</p> <p>This is why you're getting this behavior.</p> 2012-05-30T16:42:11-04:005004573http://forums.asp.net/p/1809090/5004573.aspx/1?Re+How+to+make+a+Label+control+appear+on+a+click+of+a+buttonRe: How to make a Label control appear on a click of a button <pre class="prettyprint">try this</pre> <pre class="prettyprint"><span class="pun">&lt;</span><span class="pln">asp</span><span class="pun">:</span><span class="typ">Button</span><span class="pln"> ID</span><span class="pun">=</span><span class="str">"Button1"</span><span class="pln"> runat</span><span class="pun">=</span><span class="str">"server"</span><span class="pln"> </span><span class="typ">Text</span><span class="pun">=</span><span class="str">"Button"</span><span class="pln"> onclientclick</span><span class="pun">=</span><span class="str">"return MakeLabelVisible();"</span><span class="pun">/&gt;</span></pre> <pre class="prettyprint"><span class="pln">&nbsp; </span><span class="pun">&lt;</span><span class="pln">script language</span><span class="pun">=</span><span class="str">"javascript"</span><span class="pln"> type</span><span class="pun">=</span><span class="str">"text/javascript"</span><span class="pun">&gt;</span><span class="pln"><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">function</span><span class="pln"> </span><span class="typ">MakeLabelVisible</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">var</span><span class="pln"> labelcontrol </span><span class="pun">=</span><span class="pln"> document</span><span class="pun">.</span><span class="pln">getElementById</span><span class="pun">(</span><span class="str">"&lt;%=Label1.ClientID%&gt;"</span><span class="pun">);</span><span class="pln"><br /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labelcontrol</span><span class="pun">.</span><span class="pln">style</span><span class="pun">.</span><span class="pln">display </span><span class="pun">=</span><span class="pln"> </span><span class="str">"block"</span><span class="pun">;</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labelcontrol</span><span class="pun">.</span><span class="pln">style</span><span class="pun">.</span><span class="pln">visibility </span><span class="pun">=</span><span class="pln"> </span><span class="str">"visible"</span><span class="pun">;</span></pre> <pre class="prettyprint"><span class="pln">return false;<br /><br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="pun">}</span><span class="pln"><br />&nbsp; &nbsp; </span><span class="pun">&lt;</span><span class="str">/script&gt;<br />&nbsp; &nbsp; </span></pre> <pre class="prettyprint"><span class="pun"><br /></span></pre> 2012-05-30T16:46:56-04:005005583http://forums.asp.net/p/1809090/5005583.aspx/1?Re+How+to+make+a+Label+control+appear+on+a+click+of+a+buttonRe: How to make a Label control appear on a click of a button <p>Hello,</p> <p>Client side changes will get refreshed after you post back. So you need to stop the post back behavior of the asp:Button. To do that, add return false at the end of the onClientClick event as below.</p> <pre class="prettyprint">&lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Button&quot; onclientclick=&quot;MakeLabelVisible(); return false;&quot;/&gt;</pre> 2012-05-31T09:42:33-04:005006228http://forums.asp.net/p/1809090/5006228.aspx/1?Re+How+to+make+a+Label+control+appear+on+a+click+of+a+buttonRe: How to make a Label control appear on a click of a button <p>I have something similar ready now( but in javascript)</p> <p>&lt;input type=&quot;button&quot;&nbsp; id=&quot;labelbutton&quot; onclick=&quot;gclick()&quot; style=&quot;&quot; value=&quot;text&quot;/&gt;</p> <p>&lt;script type=&quot;text/javascript&quot;&gt;</p> <p>var ndiv = document.createElement('div');<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ndiv.setAttribute('id', 'div');<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; document.getElementById('host').appendChild(ndiv);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ndiv = document.createElement('label');<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ndiv.setAttribute('id', 'label');<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; document.getElementById('div').appendChild(ndiv);</p> <p>&lt;/script&gt;</p> <p>Basically it makes a div(with id of div) and puts a newly made label(with id of label) inside the div, and the div is generated inside of an element with an id of host.</p> <p>I hope it makes sence <img src="http://forums.asp.net/scripts/tiny_mce/plugins/emotions/img/smiley-laughing.gif" alt="Laughing" title="Laughing" class="emoticon" border="0"></p> <p>This also makes as many labels as you need, doesn't have to be just one.</p> 2012-05-31T14:50:15-04:005006331http://forums.asp.net/p/1809090/5006331.aspx/1?Re+How+to+make+a+Label+control+appear+on+a+click+of+a+buttonRe: How to make a Label control appear on a click of a button <p>Thanks a lot folks. making return false works..</p> <p>Kind regards</p> <p>This is the best forum i have seen so far.</p> 2012-05-31T15:35:05-04:005006336http://forums.asp.net/p/1809090/5006336.aspx/1?Re+How+to+make+a+Label+control+appear+on+a+click+of+a+buttonRe: How to make a Label control appear on a click of a button <p>Thanks Ruchira.</p> <p>Regards</p> 2012-05-31T15:37:15-04:00