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

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"));

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

ASPX file pointed in AngularJS $route + ngView is not working properly

I am trying to use AngularJS $route + ngView to point to some ASPX file.
Part of the code is like this :
$routeProvider('/action', {
templateUrl: 'pages/action.aspx',
controller: 'mainController'
});
When I go to "/action", it correctly loads the ASPX and show the result. The problem is: when I press a button inside the ASPX file, it routes to root directory and shows the "The resource cannot be found" error. In fact it changes the path of ASPX file. How can I resolve this issue? Is it possible to use ASPX file in AngularJS routing?
I don't see how it works.
you can wrap SPA application inside ASPX file but not the other way around.
ASPX file generates a full html file ,not just partial html block.
user controls makes more sense but I don't see how to can render user control outside of ASPX framework.
Honestly I don't understand what are you trying to pull,
It makes no sense to load ASPX files inside angular framework.

The name does not exist in the current context

I have a masterpage in my asp.net 3.5 application and I have some controls and jquery stuff. I try to access the controls in codebehind and it says :
The name 'DrpStates' does not exist in the current context
Why it is not accessible in codebehind ?
When you create a code behind file, ASP.NET also automatically generates a designer file (which is right next to it). In that designer file all the controls are initialized and loaded. Sometimes (for reasons unknown) when you create a new control, it fails to re-initialize the designer file and you can't get access to the control in the code behind file.
Try doing this >
Delete the designer file (right click > delete)
Right click on the aspx file > Convert to Web Application
Should work now
It's probably part of the master page or parent page, try using FindControl method:
this.Page.FindControl("DrpStates");
There could be a problem with your .designer.cs file. Check if you have a designer file with the same name as your aspx (or ascx) file.
If you open the aspx file and switch between design view and html view and back it will prompt VS to check the controls and add any that are missing to the designer file.
Try right clicking on the aspx and select "Convert to Web Application".
You can also try deleting the .designer.cs file and then recreate an empty file with the same name.
reason : - When we create a code behind file, ASP.NET also automatically generates a designer file. In that designer file all the controls are initialized and loaded. Sometimes when we create a new control, it fails to re-initialize the designer file and you can't get access to the control in the code behind file.
There is a simple Solution to this situation.
Step1 : open the the yourfile.aspx.designer.cs file
you will find things like " protected global::System.Web.UI.WebControls.Label Label2; "
these are the initialized components in the sequence in which they were generated by you.
Step2: just copy and paste the following line repeatedly for every missing component that were not
recognized by the code behind : "global::System.Web.UI.WebControls."+Class of the component that you are missing + single space + id of the missing component.
Step3: save the file and voila all the components error disappear magically.

codebehind cannot reference the page controls

I have controls in ascx file but i can't see them in intellisense in .cs file.It was working nice before.
I can see the control names in designer.cs file.
I have deleted the Asp.net temp files in AppData folder but still not working.The other user control files in the app can reference coerrectly to it's page controls. What is the problem here ?
I use VS2008.
Look at the top line of the .ascx page and check out the value of Inherits= and make sure that is has the right namespace.class appropriate for your codebehind. For example if your namespace is ProjectNamespace and your control class is MyControl then it should be ProjectNamespace.MyControl. This can get out of synch if you renamed the ascx file, etc and cause this type of problem.
This happens to me every time I copy a user control from one project to another. The connection between the ascx and the code-behind breaks.
This solution is tedious but it gets around the problem:
Create a new User Control. Visual Studio will correctly connect the ascx file to ascx.vb file.
Copy the ascx and vb code from your original control into the new one.
Delete your original control.
You now have a working control, but it has a different name.
If it is important to retain the name of the original control, repeat the whole process again and copy the second control to a third one with the correct name.
Write to Microsoft and ask them to stop adding bells and whistles we don't need and fix the basic stuff!
I just figured this out for my situation: in the Page parameters of the .aspx file, the 'Codefile' parameter was pointing to the file name: 'LabEdit.aspx.cs'. It should have been pointing to the path: "~/WOPages/LabEdit.aspx.cs". I know I didn't move the codefile or the page file, so this is maybe a problem with VS2008
I have solved the problem, I have uninstalled and reinstalled VS2008 and it is solved.

how to use codebeside in ASP.NET Web Application

I'm using VS2008 and want to create a web application (not a web site) with Code-Beside
but, the default mode of aspx is Code-Behind.
I have tried to change the CodeBehind=ClassFile.cs to CodeFile=ClassFile.cs in the header of aspx's <%#Page%> part, and deleted the aspx.designer.cs file,but if I added a server control to the page, the compiler is also send me an error of no member defined.the cs file is the orinal file of codebehind, it is partial class.
You don't want to delete aspx.designer.cs you want to delete the aspx.cs file, then place a similar file next to it and declare it as a partial class. designer.aspx.cs is still required to provide you direct access to controls placed within the page, rather than going through FindControl.
You definitely don't want to delete the .designer.cs file, as this is where the server control definitions will be placed.
In general the codebehind model is much better as it makes the code easier to find, use and maintain.

Resources