change Visibility of a control in ASP.NET code behind (c#) - c#

How do you set the visibility of a (fileupload) control from ASP.net code (I need to hide a fileupload control in a webuser control from server site, otherwise hasFIle is always false).
Also setting the "Visible" property to false does not work (as is confuses the AJAX panel so the fileupload forgets that it has a file).
theFileUpload.Visible = false => does not work
so I want to try to set the CSS style visibility to hidden or display to none.
The main problem is I want to do it from the server side (I know how I could do it on client).
Is there a safe way to overwrite
theFileUpload.Attributes["styles"]
in case I modify other CSS styles in there,
also throwing a whole CSS class at it (by moidifying the CSSClass property) seems like overkill.
thanks in advance
Axel

By using theFileUpload.Visible = false; you just tells to asp.net to not render theFileUpload on the page.
You may use
theFileUpload.Attributes.CssStyle[HtmlTextWriterStyle.Visibility] = "hidden";
That allows you to set only a specific css property.

Related

How go I get the javascript in my user control to work inside of a panel whose visibility is false on page load?

I am working on an ASP.NET/C# project in which we are placing a user control into an AJAX panel whose visibility is set to false on page load. The visibility of this panel is set to true once the user submits some parameters. However, although the user control and all of the other panel contents are visible once these parameters are set and submitted, the uc's javascript functions do not work.
I attempted to solve this issue by adding Page.Controls.Add(controlID) in Page_Load if it is not a postback. The functionality is all there when I do this, but I know that there is a better way to get the functionality because in this case the control is being added to the page twice.
EDIT: I put the controller in a div outside of the panel and got the control that I wanted out of it. However, if anyone can explain why the javascript of the control wasn't defined or offer a solution that keeps my UC in the panel, I would really appreciate it.
If for asp server control the visible property is set to false, then that control is not even rendered on the page. Since the User Control is inside the ajax server control, the user control is not rendered. Javascript code is executed by the browser hence it must be present on the page to run.
If you can set css property display: none to hide and display: block to show for the ajax server control, then user control and its javascript will be rendered on the page, however hidden from user for css property i set to display: none, so its javascript will be executed.
To set display property of ajax server control, you could use: ajaxControl.Styles.Add("display", "none").
Hope this helps you.

ASP.NET Custom Server Control not disabled inside a Panel that is disabled

I have written a custom ASP.NET Server Control. When I am rendering the control, I check the this.Enabled property to determine if I should add the disabled attribute to my tag (extract of code below). Unless I specifically set the Enabled flag, this value is True regardless of the state of the panel it is in.
output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
if (!this.Enabled)
{
output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
}
output.AddAttribute(HtmlTextWriterAttribute.Value, this.DisplayName);
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
Standard server controls like textboxes etc, behave as expected in this scenario and are disabled.
What is the pattern that I must implement to be able to check if the control is actually to be disabled or not? Do you have to check the parent(s) to see if any of them are a Panel and then see if they are enabled? Seems very inefficient if that were the case.
Thanks
Mark
Just found it.
Needed to modify the code to:
output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
if (!this.IsEnabled)
{
output.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
}
output.AddAttribute(HtmlTextWriterAttribute.Value, this.DisplayName);
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();

How can i check the panel status

How can I check the current status of an asp panel? I want to check the current status of the panel as to whether it is enabled or disabled. If it is enabled, then i'll do something in the code behind. Please help me to check it..
From code behind side you can check it as
if(PanelName.Enabled)
If it returns true that means it will post back.
I think you're looking for WebControl.Enabled property:
var isEnabled = panel.Enabled;
When the Enabled property of a control is set to false, the control
typically appears dimmed. If the control is an input element, the
browser prevents the user from clicking or typing in it. HTML elements
that are rendered for a server control are marked as disabled by
setting their disabled attribute or their CSS class attribute.

Cannot change SkinID property dynamically

I get this error when I try to have my C# class change the skin of an asp control:
The 'SkinId' property can only be set in or before the Page_PreInit
event for static controls. For dynamic controls, set the property before
adding it to the Controls collection.
My goal is to provide a panel, call it ID="response", on every page, and then dynamically change it's CSS class from Error to Success, or Success to Error (so it's red or green). And also I make it visible = true, when a response is created.
Apparently, I am forced to use CssClass attribute, which is the only way this will work.
As a side-off-topic note:
In PHP, you would not have a problem of using different "pre-init" "post-init" etc. A completely unnecessary process. You would simply change the html before you send it back to the user. I'm a bit confused why ASP.NET decides to overcomplicate everything. It's a bit silly for me to take time to learn all these different complicated processes to simply display a webpage. It takes time to learn all the quirks written in difficult-to-read ASP life-cycle documents on microsoft. Not to insult any microsoft people, but it's just not practical.
If it is a static control, that is you are defining the Panel in your .aspx page, then the only place to change the SkinId is in the PreInit method e.g.:
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
String panelSkin = ViewState("panelSkin").toString();
panel1.SkinId = panelSkin;
}
Of Course, the PreInit method is only called when the Page is being first Initialized -- not on a PostBack.
You could save the skinId you wanted to use to the ViewState and then call a Response.Redirect("myPage.aspx")... and as seen above grab the skinId string from the ViewState and set the Panel skinId accordingly.
Alternatively, rather than using a Panel try using an UpdatePanel from the .Net Ajax library. Clicking a button in the UpdatePanel (provided it's setup to Trigger an ASyncPostBack) will run the OnPreInit method.
That said, provided you are changing the background, going with the CssClass property would be the most efficient way to do this.
ASP, and its child ASP.NET, is basically a huge hack of vanilla HTML and the IIS page renderer. It hooks into various stages of the lifecycle that already existed in IIS, rather than having its own lifecycle like PHP. As such, there are things you can do in certain areas because the things it depends on either aren't set in stone so you can change them, or are so you can work with them. The great power of ASP.NET, which is the interop with .NET classes and the .NET Framework, IMO makes up for some of its idiosyncracies.
Anyway, Skins are part of Themes, which are loaded early in the process so the controls can be initialized with their proper default Styles. That's the key; the Theme is locked after PreInit, but the Styles (and CssClasses) behind the Skins are editable right up to and including PreRender, which includes event handlers (which fire validation). So, set the Style or the CssClass dynamically.
To do it without a full postback, you can put the controls that should change color in an AJAX UpdatePanel, which can be re-rendered separately from the other elements of the page and will keep its current contents until the DOM is modified via the JavaScript client-side.
Setting the CssClass attribute is much closer to what you'd do with PHP, so why not just do that?
The two real benefits of Skin files are setting defaults for all controls (no skinId at all) or setting properties that can't be controlled with css.

stop setting onclick event for asp:LinkButton if no OnClick event was explicitly defined

How do I setup a default setting so that if I do not set an OnClick (i.e the asp.net OnClick attribute) explicitly for an asp:LinkButton tag, it will not render an onclick(html attribute for javascript) attribute client side? By default, asp.net adds an onclick='doPostBack....' for the LinkButton.
Case for use:
There is a LinkButton tag on the page. For this page, if the user has one friend, I only want to run client side code if the button is clicked and would not for any reason want to make a post back. If the user has more than one friend I would want a click to trigger a postback.
Solutions that include the following are not helpful:
Using any asp.net Ajaxtoolkit
Dynamically switching the control type (i.e. if friends == 1 use a asp:Hyperlink)
-I want to avoid this because it is not scalable. There might be many cases where I want an asp:Link tag to do a postback or to not do a postback depending on the user context or user attributes
Using OnClientClick (I am using jQuery would like to avoid this)
Solution that would be helpful if possible:
If I could see server side at runtime whether an OnClick event was explicitly set on an asp:LinkButton tag, this would solve my problem, too. any ideas?
How about rather than dynamically switching the controls (as you mentioned is a solution you don't want), you could always use an asp:HyperLink and set the NavigateUrl property to redirect your page back to itself with a query string of some sort indicating what was clicked.
If you don't want the post to happen at all, simply leave the NavigateUrl property blank.
Of course, this will be pretty worthless if the rest of the page is dependent on ViewState and such.
http://forums.asp.net/t/1129106.aspx
This link explains how to see server side at runtime whether an OnClick event was explicitly set using reflection

Categories

Resources