asp.net how to use sever side changes after post back - asp.net

In my asp.net application I have a some client side scripting that alters the page events and do different things. One problem I am having is when I post back to the server after all of the validation is done on the client side, I have a couple more validation checks on the server side (c#), and I want to show the proper error, but the page refreshes. I just would like to do some server side coding and if i need to go back to the page, go there as lasted viewed, with the error in the label, and changes i made on server side. Thanks for any help.

I think you must post it in ajaxy fashion.
I mean use ajax to post..
Evaluate the results on client side using JS.
Then if everything is fine do a redirect.
if errors exist you are still on that and can modify the html .

Do your server side validations in an AJAX call instead of a normal page post back. Because, you are making a background server call, your page does not refresh and on validation failure, you can retain the browser side UI. If validation is successful then you can do your regular post-back for actual processing.
From implementation perspective, you can use Page Methods (or JSON WCF services) in ASP.NET AJAX for the same: see this tutorial to get started with Page Methods: http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx

Related

Javascript to handle a form return

I have a standard html registration page that targets an external .asp page on submit.
What happens is that currently the .asp (which I don't have access to)returns an entire html page.
Instead I would like to somehow parse the returned html and populate the existing form with either
a) validation errors if incorrect
or
B) some sort of success message if all validated
Can anyone tell me if this is possible and or help with some pseudo code?
This is doable in JavaScript using ajax, but it requires that the ASP page (presumably on a different domain) sends appropriate CORS HTTP headers. Even if you don't have access to the actual ASP page, you may be able to get someone to setup the headers in IIS on their server.
Otherwise, you're stuck moving everything server-side, i.e. simulating the POST on your own webserver, and scraping the HTML to get the status back. That looks something like:
Postback the page to your own page (or use Ajax)
On your server, initiate a web request post of the data to the ASP page
Parse the results in your server code
Return an appropriate response to the browser client
The best you can do, assuming I'm interpreting your question correctly, is "scrape" the HTML returned from the asp page and make proper assumptions about the location and meaning of the text within the markup. I, personally, would strongly advise against developing anything of any kind of robustness based on what amounts to screen scraping, especially considering you don't have access to the .asp file itself. If I've misunderstood your problem, my apologies.

How to trigger HTTP PUT request without Ajax?

Regarding to a static link on a web page, the browser will issue GET or POST request to the web site, depending on whether a form of parameters attached.
However, I want the browser to issue a PUT request for that link, how can I do that? I know that Ajax could do it, but I don't want to use Ajax.
I want the browser to issue a PUT request for that link
it seems that PUT and DELETE are currently unsupported in html forms, according to this submission to the w3.
I know that Ajax could do it
Not always true. Because PUT and DELETE are at times unsupported by some browsers, ajax cannot consume them without making a dummy param to trigger a real PUT or DELETE server side, which gives the illusion of full HTTP support by ajax.

How server side events are called in .NET

I am a beginner to .NET, I have some doubts in my mind. Can anybody help me to sort out?
When a user requests for a file(*.ASPX), The request first goes to IIS server and with the help of Handlers and modules it finds the type of file that need to be processed and sent back to the client. But while displaying on the cilent machine the content of the .ASPX file will be displayed as HTML controls. How are the events generated at the client side and sent back to the server?
I know runat=server tells the control will be processed at serverside.
But every time why we need to write "runat=server". Is there any ASP.NET control which runs at client side?
When you tell a "tag" to runat="server", you tell the server to register the tag. Now the tag is included in the control/tag tree on the server, and can be found ont the server: When an event is fired, or attributes or contents is changed server side.
Client side events, are silently converted to a form POST. Together with some extra information, like the ID of the control, and the type of event.
Since your tag is registered on the server, it has an ID, that is also transferred to the client (look at the html source). This tree is build, (by parsing the .aspx file) before the event is "fired". Using this id, you can find the tag in the server-side control/tag tree, and run the method that is used to handle the event.
Using these technique, and a lot of ViewState, Asp.Net tries to deny the stateless nature of HTTP. At first this looks like a good idea, IMHO its better not to fight with nature. Currently ASP.Net MVC feels more naturally.
if you are starting with .NET web development, ASP.NET MVC is the way to go IMO
an ASP.NET server control that is generating client events in the browser is exercising standard dom events via javascript code that gets injected into the page that is sent from the web server. try view page source from any browser to see what is actually generated and the picture will become more clear.
essentially runat=server is telling the ASP.NET parser to process the tag and generate some special HTML output for the page. see GvS's explanation in this thread of how client events for these controls are converted to a form POST that is handled on the server.
http://www.w3schools.com/aspnet/aspnet_intro.asp
How Does ASP.NET Work?
When a browser requests an HTML file, the server returns the file When
a browser requests an ASP.NET file, IIS passes the request to the
ASP.NET engine on the server The ASP.NET engine reads the file, line
by line, and executes the scripts in the file Finally, the ASP.NET
file is returned to the browser as plain HTML
also see
http://www.w3schools.com/aspnet/aspnet_controls.asp
http://www.w3schools.com/aspnet/aspnet_events.asp
These event just raised by server-side controllers ,when you click on a asp button , event information be captured on the client and an event message transmitted to the server, via an HTTP post.i think these possible by some registered event on the server.when you page is loading , some events registered on you page framework , and the raised by http post request.
Look, one of the difference between the client side and server side controls is the server side are registered in server and you can save their state in a view state for example. when you click on a server side button , first its client side event raised and the the server side .note that because the server side controls event registered on the server , when you click on the button ,you page posted back.but the client side controls just are valid in client , and their event just raised for you web browser
need more help ? comment me to edit my answer
regard , Ali

Partial PostBack without AJAX

I want to silently check for Session existence without Posting back page by using AJAX Timer and AsyncPostBack Trigger.
Now, I would like to know is there anyway to silently check whether ASP.NET C# if (Session["email"] = null) { Response.Redirect("Logout.aspx"); } something of this kind to check for every 10 seconds without Posting Back Page and without using AJAX by using something like jQuery or any other technology that is supported by .NET?
The server can push to a loaded page if you use an asynchronous controller with a partial view which is loading every set amount of time.
http://msdn.microsoft.com/en-us/library/ee728598.aspx
there is only one way to get to the server. send a request. that can be a "standard" request where the browser will refresh the screen when a response is sent. the other type of request is a ajax request. the difference is an ajax request contains a header to inform the server it's an ajax request. when the browser receives the response it will allow the developer to decide what to do with the response. either way a full request/response exchange takes place the only difference is how the browser handles the request.
jquery is a javascript library that includes functionality to make ajax request easier to setup.
to answer you question no, there is no way around making a request.

Javascript, Ajax, load data from DB dynamically

I have for example ASP.NET site with textbox, button and many other controls.
When user click on button i want to retrieve some data(string,number) from DB(MSSQLServer) and show these data to user without postback(ajax).
How can i do this?
Thanks
There are two key components. A server-side method that can respond to a request for data and a client-side javascript function that can make an AJAX request to that method and consume the data, updating the web page.
On the server side you can use an actual web service or web application methods marked with the WebMethod attribute. These accept a request and respond, not with a web page, but usually a partial HTML snippet, XML, or JSON.
On the client side, I would look at using a framework that implements AJAX -- such as MicrosoftAjax or jQuery, though there are many others. It would be then be a matter of correctly configuring the client function to respond to the interaction trigger (button click) so that it calls the proper method with the right parameters and processes the response.
Partial Page Updates vai AJAX
Using Web Services via AJAX
jQuery -- many examples and documentation

Resources