ASP.NET: Getting ASPX filename - asp.net

By convention, all my web site's .aspx files also have corresponding .css files at the same path. So, for example, Default.aspx has a file Default.css in the same directory.
I wrote an extension method to add CSS tags to the headers of Page objects, and use it like this on Page_Load:
this.AddCssFileRange(new[]
{
"Default.css",
"../Master.css"
});
I'd like to replace the hardcoded "Default.css" with a method that derives this based on my CSS convention. That is, I'd like to replace it with a method that returns "Default.css" because the filename of the Page calling it is "Default.aspx."
How can I retrieve that "Default.aspx" filename so I can replace the extension with "css"?

Like this: Path.ChangeExtension(Request.CurrentExecutionFilePath, ".css").
This will return the currently executing page, even if you called Server.Transfer.

Related

Using application in html with asp

I'm working on a website with ASP and I'm using this code to transfer variables from page to another:
Application["source"] = "testpage"
This is used in .cs file, how can I use 'Application' and set a value in 'source' from html code?
So you want to use the Application object from the source code of an HTML files instead of the code-behind (the .cs file)? The short answer is that you can't.
The Application object is one of the built-in objects of ASP and can only be manipulated with code that runs on the server. The HTML file is rendered on the client (the web browser).
You could show the contents in the browser by using code like the following in the .cs file:
Response.Write(Application["source"].toString());
This should print "testpage" in the browser.
Use this in controller:
ViewData["source"] = "testpage"
And this in View:
<label>#ViewData["source"]</label>
Also, you can use ViewBag.Source instead of ViewData["source"] both in Controller in View

problem with relative path to .js file in ASP.net ScriptManager

I'm working with an ASP.net web application.
I've written a user control called LocationSelector that has its own Javascript in an external .js file. In order to load that file, I use the following line of code:
ScriptManager.RegisterClientScriptInclude(this, typeof(LocationSelector), Guid.NewGuid().ToString(), "Controls/LocationSelector.js");
The problem is with "Controls/LocationSelector.js". As long as the page that uses the control is in the root directory of the application, everything works. However, as soon as I try to put this control in a page in a subdirectory, it can't load the Javascript file.
How can I fix this?
Haven't tested it, but off the top of my head I would say you need something along the lines of this:
ScriptManager.RegisterClientScriptInclude(this, typeof(LocationSelector), Guid.NewGuid().ToString(), Page.ResolveClientUrl("~/Controls/LocationSelector.js"));

Accessing a control in asp.net content page through Javascript

I have a form in my content page for which I am doing some client side validations via Javascript. The Javascript behaves as expected if I place the JS code directly in the content page. But if I place the JS code in it's own file and try accessing that from the content/master page (through the script tag's src attribute), I get a run time error when the validation function in JS being called.
To be specific, I get the below error.
Microsoft JScript runtime error: Objected expected/required at this line - document.getElementById('<%=txtemailId.ClientID %>').value
txtemailId is in the content page.
Javascript code is placed in validation.js and accessed via master page.
The reason I guess is that when .net is parsing the files, it is unable to substitute txtemailId.ClientID with the client side value that would be generated later on. So, how should one go about it? Thanks!
The answer to this is quite simple.
Within your content page declare an in-line JScript variable, make sure this is above your tag.
<script>
var emailClientId = <%=txtemailId.ClientID%>;
</script>
Within your include.js file make use of the globally scoped emailClientId variable.
Obviously this is a bit clumsy because not all of the JScript code is contained within the include.js file, which makes it difficult to debug / diagnose as it is not clear specifically where the value is coming from. You should clearly comment / document this within your code to make maintenance of he codebase easier in the future.
You're right, the code <%=txtemailId.ClientID %> will only get replaced with the real client ID by ASP.Net if this code is in an ASP.Net file. So if you put this javascript in a separate .js file, ASP.Net will know nothing about it, and the code will not be parsed.
The easiest way to achieve this is to make the control IDs parameters of your functions. This way you can have the calling javascript code in your .aspx (or .ascx) file, along with the <%=txtemailId.ClientID %> code.
Standalone .js files are not run through the ASPX parser, so your expression is not being evaluated.
To solve the problem, you could set global variables (or function parameters, or something else) in the ASPX files with the IDs of the controls, then use them in the standalone .js file.
Alternatively, you could uses class names (which don't get mangled) instead of IDs. (This is very simple using jQuery)
An approach like this is best suited for something you want to achieve:
validation.js
var Validation = {
EmailId: null,
Validate: function() {
var email = document.getElementById(EmailId).value;
}
}
page.aspx
Validation.EmailId = '<%=txtemailId.ClientID %>'; //initialize
Validation.Validate(); //whenever you want to validate

creating the code behind file

First i created the TimeSheet.aspx file,then added code files names TimeSheet.aspx.cs file.
created a TimeSheet class and code some code in that ,later in the #page directive added the codebehind attibute with value "TimeSheet.aspx.cs" and inherits attribute with value TimeSheet.
Now i want to make this code behind file to show up as a sub-branch of TimeSheetp.aspx.Just like a designer file.
Like
TimeSheet.aspx
|----TimeSheet.aspx.cs
how will i do that.please help me
At the top of the Solution Explorer, toggle Nest Related Files.
If you did not let VS create the file for you when you first make the file, you just need to create a new file (class file, '.vb' or '.cs' extension) in the same directory as the file you wish to add the code behind file to with that file name as the first part of the new file name. Example, default.aspx's code behind file that you create manually will be called "default.aspx.cs". Then in your original file, make sure you add a page directive for the newly created file: <% #Page Language="VB" Explicit="True" Codebehind="default.aspx.vb" %>
The Page directive should be configured automatically and the code-behind created when you create your page in VS.
If you are using web forms, you shouldn't have to create the .cs file; it should create it for you. Right click the aspx and do view code to see the code file. if you are in an MVC project, there isn't any code-behind, but a controller file. And so creating the cs file doesn't do anything.

asp.net and Ajax

I am using the following JavaScript code to refresh another page
window.opener.location.replace(url)
The problem is when entering the URL, do not find the page as the page is located in the root and this calling code is placed in a page inside another folder. How do I specify the path to point to the root, which is where the page is located? I have tried many things, but none worked:
//page.aspx
../page.aspx
~/page.aspx
page.aspx
/page.aspx
....
This form should work:
window.opener.location = url;
The replace method replaces a match in the current URL with another string, but it won't work with just one parameter. At any rate, it is not necessary to specify the full URL, you can only specify the partial path such as:
../page.htm
You would have to give exact path

Resources