Click here to Skip to main content
15,506,599 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi I ma trying to set the control hidden through C# as the javascript and Html does, see below example

Javascript (
controlId.style.display = 'block'
or HIde);

Html
HTML
"style display:none"


so same thing i tried with C# code but did not get succeed, through below code I am loosing my control at run time.

html1.Attributes.Add("style", "display:none");

ButtonAssociateRules.Style["visibility"] = "hidden";


this code completely removes my control form the running page, That I don't want because my javascipt is using that control after some time, and I am getting null, because C# removed that control, I have seen the source code at run time.


Please help.
Posted
Comments
Sergey Alexandrovich Kryukov 12-Jun-14 3:13am    
No, this code won't remove your control. Look properly at the page source you obtained after the request. Your problem is not the code, but inability to see its result, and only then the code.
—SA

You can find many articles around this topic. For instance this link is completly helpful to you:
Hide and show control in ASP.Net
[^]
 
Share this answer
 
v2
If i understood you correctly, You are trying to hide control at Server code and then at some point of time you are suppose to access it client side. If so, try this one:
your_control.Style.Add("display", "none");

Remember,this will render control at your page(Not remove), so it will be available at client side also.E.g. if its div element,you can modify its html even its not visible to you
C#
$(document).ready(function () {
        //////////////////
             $("#your_control_id").html("......");//at this time control is hidden still you can access it
             $("#your_control_id").show();
             return false;        
     });

Though this is JQuery, i just showed you how to access it client side.
 
Share this answer
 
use css class like below
CSS
.hidden {
   display: none;
}


then you can set it as
C#
html1.CssClass = "hidden";
 
Share this answer
 
hiding a control in C# and ASP.Net is easy.
simply add the attribute runat="server" and id="id" to the control for example,

ASP.NET
<div  runat="server" id="samplediv" style="height : 100px; width : 100px; background :red;"></div>


now in code behind it is simple to hide the control on either page load or button click or on whatever the event you like to use hide the control.
hide control on button click
C#
protected void btnHide_OnClick(Object sender, EventArgs e)
{
samplediv.visible = false;
}


hide control on page load
C#
void Page_Load(Object sender, EventArgs e)
{
samplediv.visible = true;
}
 
Share this answer
 
v2
Comments
Thanks7872 12-Jun-14 3:01am    
If you set visible="false", control will not be accessible at client side.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900