ASPX file pointed in AngularJS $route + ngView is not working properly - asp.net

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.

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

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.

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

How to create another page class in a aspx c# page

Results r = (Results)Page.LoadControl("Results.ascx");
In my Page i cannot access USER CONTROL CLASS(Results)
Gives error.I cannot resolve.
No namespaces at all.
Even i cannot access other aspx class in same folder's other page also..
Can you please help me.
ASP.NET compiles pages in batches, and there's no way you can be sure that your currect page is compiled with the Results page. You need to define an interface that you store in App_Code (which all pages are compiled with), implement this interface in your Results page, and use the interface to access whatever you're trying to do.
You could move logic from your Results page into your domain, or into a helper class, instead of keeping it in the view (the page).

Resources