Using application in html with asp - asp.net

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

Related

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.

Html in ASP.NET server control

Is there a way to write pure HTML when i'm developing my ServerControl in ASP.NET?
I want to create my control as .dll file. But when I'm writing my control I can only add HTML tags and attributes in C# in my .cs file.
And so i am forced to use this in my RenderContents() method:
output.RenderBeginTag(...
output.AddAttribute(...
output.RenderEndTag(...
instead of for example:
<div attribute1="value"></div>
is there a workaround of this problem?
Unfortunately only user controls (ASCX) allow for this type of approach (delcarative markup) but they cannot be packaged into DLLs. I've always hated this restriction, to me it's a code smell programmatically generating markup, but this is what we must live with!
If you have very static markup you could always put it into a resource file (resx) instead of outputting the HTML programmatically

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.

how to use NVelocity from asp.net webforms?

I want to use "NVelocity" from plain ASPX pages without using any MVC framework. I don't want to use "NVelocity View Engine" thru' asp.net MVC framework. The only example that I got for "NVelocity" is for merging and writing onto console window (http://www.castleproject.org/others/nvelocity/usingit.html)
I am looking out for example on to integrating "NVelocity" into aspx web forms. Any pointers would be really helpful.
I found a way. The idea is the override Page.Render() method in an aspx page. Write the code in Render() method to transform the HTML template (I mean, *.html file or *.aspx file) using NVelocity. Pass HTMLTextWriter object while merging the template and context "template.Merge(context, writer);"
This will render the transformed HTML to web browser.

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