how does asp.net code behind work? - asp.net

i am new to ASP.NET development, and i am a little bit curious on the asp.net code behind mechanism.
i know why we use it
but what i would like to know is:
the relation between aspx page and the code behind
who does the linking between these two separate files ?
how does it work?
how an element created in aspx page can be accessed directly from code behind ?
I mean what happens BEHIND THE SCENES ?
thanks

Related

Vb aspx project, I need help to understand the logic behind

I'm a java programmer and for the first time I need to face a VB and ASP.NET web project.
I found some very basic tutorials on how ASP.NET works but I didn't understand very well how the logic behind works.
This project consist of lot of coupled files, the main pattern I found is:
file.ascx
file.ascx.designer.vb
file.ascx.vb
file2.aspx
file2.aspx.designer.vb
file2.aspx.vb
How do these file work and interact? I'm trying to understand it in a MVC logic but I can't seem to get it.
Drop the MVC logic in your head. ASPX doesn't use the MVC (at least by default).
The code files you see are grouped in two:
ascx: markup file. On the fly converted to VB.NET > MSIL;
ascx.vb: the code behind file. This one is merged with the generated code from the markup file (thanks to the partial keyword in the class declaration).
The ascx file is a control file, the aspx file is a page file. A page file can consist of zero or more controls, defined by the ASP.NET team, third party developers or you. If you want a custom control, you can create an own control by creating an ascx and ascx.vb file (or let Visual Studio do it for you).
aspx files usually will have the UI and will which is usually HTML tags, some ASP.NET server control embed code. aspx.vb file (codebehind) will have server-side coding in VB.NET.
In the MVC logic, you can relate aspx page to View and aspx.vb to Controller action methods.

Could I create ASP.net page method in web form code behind file?

Could I declare asp.net page method in side Web form code behind file using [System.Web.Services.WebMethod] attribute?
Yes, you can add the code inline if you want - it's pretty messy though. Take a look at this blog post for help. Also, take a look at this MSDN reference for a comparison. There is also another tutorial here.
In my opinion, if you need to use webforms, then you should stick to the code behind model and keep the minimum about of code in there for the event handlers only. Move any business logic, or utility code into separate classes.

Checkboxes in ASP.NET

I am trying to use a checkbox that is dynamically declared in an .vb file that I am trying to write into my .aspx page. I am able to write a normal checkbox of <input type='checkbox /> from the .vb Class using Response.write, but it comes up blank when using <asp:Checkbox runat='server' />I need to pass whether or not the box is checked back to the server, because I am having to either approve something if one is checked, reject something if the other is checked or do nothing if neither are checked. I have figured out how to make them mutually exclusive either way so that is not the problem. Does anyone have any recommendations?
Your problem lies in the order that the pages are compiled in: When you place an asp control like the asp:checkbox, it is compiled into a regular checkbox with some javascript attached when it is sent over to the client.
When you write the string "<input type='checkbox />" to the page from the code-behind it is writing that string directly to the page, after the aspx page has compiled its controls, but since that is valid html the browser renders the control. When you write the asp:checkbox, the browser doesn't know what to do with it, because it is not valid html. In addition, the page has already been compiled, so there is no chance of .net creating the correct control for you.
You need to programmatic add the control to the webpage by creating a new control through the code behind
This site does a great job explaining it
And #toodles seems spot on. Writing static html and asp.net are two totally different ball games. I would spend a bit of time (like hours/days) reading/watching learning material to help you get on your feet.
The technical answers you are getting are all good. However, your question indicates that you really need to start by learning how asp.net server controls work. I suggest spending a couple hours watching the videos at http://asp.net and particularly http://www.asp.net/general/videos/intro-to-aspnet-controls
Then focus on understanding the page lifecycle and you'll have enough of the basics to be much more effective at asp.net. Have fun!
You can't use response.write to create server controls.
See this site for an example of the right way to do it:
http://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx

why we use code-behind approch in asp.net?

i want to know that why we use code behid approch. if there is some advantage then plese share to me ? as we know that there is two methode for writing server side code for
aspx.file
code-behind approch in which we usually we seprate our server code aspx.cs file from aspx file
we can directly write server side code in aspx page by writing //server side codebefore html tag ?
so i wnat to know that is ther any authentic reasion which provide advatage in code-behind approch.
You should strive to separate UI layout from UI or business logic. Those who use codebehind benefit from the advantages of compile-time warnings, type safety, and better debugging support.
Check this article - https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-1049844.html
EDITED: this one is helpful too to give information of code-behind pages -
http://www.java-samples.com/showtutorial.php?tutorialid=1084
It helps prevent spaghetti code with a mix of markup and code, which is hard to maintain and review.

How to implement A simple AJAX call using asp.net page?

I'm trying to implement this specific example using asp.net page instead of asp page.
If you look at the example you can see that there are 2 parts for the page:
Mail asp page. This page have JS code that calls other asp file for AJAX use.
the other asp page which holds the JS code.
The responseText of the call is the client side code, so, when I write something like this:
<html><head><title>test</title><script language="javascript" runat="server"
type="text/javascript">function test(){Response.Write("This is a Test!");
</script><body onload="test()"></body></html>
the page ignores my server side code and returns this:
<html><head><title>test</title><body onload="test()"></body></html>
what should I need to do to make him process my JS code and return its output?
Thanks in advance,
Oz Radiano.
asp.net does not process javascript server side, so setting the script tag runat=server with language="javascript" will be mostly ignored.
I think if you change it to "JScript" it will work, however, this has nothing to do with ajax.
"runat = server" says, preprocess this on the server and don't send it to the client.
If the language is a processable one it will be evaluated as well.
Try implementing the example after watching some videos from http://www.asp.net/learn/ajax-videos/ and http://www.asp.net/learn/ajax/
Its very easy to implement AJAX in asp.net then ASP. I can clearly give you the correct source code. :) But you seem to try out new things. Let us know how it goes!
Thanks for your responses, they made me understand that I'm not sure what my problem is.
After failing in implementing this exact example, I've googleed "how to run asp code using ajax"
this result returned and made it very clear.
Thanks again.

Resources