What is the Page.ResponseEncoding default value? - asp.net

I knew that what is the purpose of the ResponseEncoding.
And I knew I can set it in the Page directive like below.
<%# Page Language="C#" ResponseEncoding="UTF-8" %>
But What is the default value of it when it is ignored in the Page directive? thanks.

I dont think there is any default value.
The MSDN says:
Remarks:
In most circumstances, do not set this property in code. Set the
ResponseEncoding attribute to the value you want using the # Page
directive in the .aspx file. When the page is requested, the
dynamically generated class sets the property.

Related

Dynamically access master pages properties

We can change masterpagefile for particular page dynamically.but how can i access their properies which are changing as per the master pages.
what code will give me their properties.
Thanks
You can add a public property to the code behind of your master page, like so:
public string MyMasterPageProperty {
return "my stuff";
}
Then, on the content page, you can use this code to access your new property.
Page.Master.MyMasterPageProperty;
If you don't want to add the MasterType directive on the aspx page, you can always explicitly cast the Page.Master to the type of your master page, so that it looks like this:
((MyMasterPageClass)Page.Master).MyMasterPageProperty;
You can read more info here:
https://web.archive.org/web/20210513005959/https://www.4guysfromrolla.com/articles/013107-1.aspx
You'll need the MasterType directive on the aspx page:
<%# MasterType VirtualPath="~/Site1.Master" %>
Then on your aspx.cs page, you can call the properties of the master:
Master.Property1 = "whatever you wanna do";
If you need more instructions:
http://dotnet.dzone.com/news/back-basics-%E2%80%93-using-mastertype

Dynamically registering controls in .NET

Can anyone tell me if I can dynamically set the file name when registering a user control please, for example:
<%# Register src="[file name]" tagname="WebUserControl" tagprefix="uc1" %>
No, I don't believe you can. What you can do is register all of the possible controls that you might use on the page, either in the page directive or the web.config.
EDIT
What you can do, if this helps, is to add the controls dynamically in code-behind using the LoadControl method. This way, you can create instances of whatever user controls you want without worrying about registering them in the page directive or web.config. Thanks #Gabriel for pointing this out.

How to use outputcache in a usercontrol, with control properties

I have a UserControl, which should only change based on 2 URL parameters.
The problem is, it has a public property, which is used in the calling pages, so it throws a NullReferenceException on my property.
Any ideas?
I think I've figured this one out, it seems to be quite tricky which is due to my lack of comprehensive understanding of how output cache works I suspect.
You can't cache the UserControl if it has variable properties that dicatate it's content. You need to put a cache control in the Content page that holds the control. Then add the cache to the content page:
<%# OutputCache Duration="120" VaryByControl="JobList" %>
Where the vary by control is the ID of the control you wish to cache. Then specify a property for that vary by control:
<%# OutputCache Duration="120" VaryByControl="JobList.LoggedInUserID" %>
This seems to work for me!
Check the VaryBy options,
try to take a look to this articles:
http://msdn.microsoft.com/en-us/library/hdxfb6cy%28v=vs.71%29.aspx
https://web.archive.org/web/20211020113508/https://www.4guysfromrolla.com/articles/022802-1.aspx
http://weblogs.asp.net/stefansedich/archive/2008/03/17/output-cache-with-usercontrol-on-masterpage-and-multiple-varybycustom.aspx

Why is #MasterType directive not implied when setting #Page masterPageFIle?

The docs for #MasterType have this example.
<%# Page masterPageFile="~/MasterPage.master"%>
<%# MasterType virtualPath="~/MasterPage.master"%>
Why is #MasterType even needed? Couldn't the compiler automatically take the same actions based solely on #Page masterPageFile? When would you not want to use both?
You can set different master pages at each pages PreInit. So the master page is in general not well defined.

Substitution Control and Cache Location

If I use Substitution control in asp.net page, and also add the following directive to the page:
<%# OutputCache Duration="7200" VaryByParam="None" Location="Any" %>
Would the location attribute be ignored, since using Substitution control on a page makes the page cacheable only on the server?
Yes, it will be ignored. Substition.Render calls, through RenderMarkup, HttpResponse.WriteSubstitution that calls HttpCachePolicy.SetCacheability(HttpCacheability.Server).

Resources