No way to interact with controls through webmethod? - c#

I'm starting to believe there is no way for me to interact with asp controls from within a webmethod. I wouldve thought i could some how accomplish this, by finding the page the webmethod was called from, and from there find and update controls on the page.
But after having an open question for 3 days and numerous google searches, it seems that there is no way for me to do this.
Or is there? I would really appreciate if someone could provide some useful information for me on this matter.
The problem is quite simple from a perspective.
1. I'm calling a webmethod through ajax, this is happening on say page A.
2. After a succesful ajax call, i wish to update an ASP control on page A.
3. The update of the ASP control must happen without a postback hence the ajax.
Is this really impossible?
Also if you know anything about this matter, but you don't understand my question, please have a look at my other active question: Update object on masterpage through ajax webmethod

When you call a webmethod, what happens is quite different compared to a button click for example in asp.net webforms.
The webmethod doesn't construct all the controls as a standard click event does.
So that's why you can't have access to the page controls.
Also, how would this work even if you had that access? Your webmethod only sends back some data, not the entire html code, so there's no way to update a control's value server side, since it has to be rendered in html.
If you want to update the value of a control on the client side (webbrowser), you can only do it via javascript when you receive the result of your webmethod. You only have to find the control by its id, and update its value.
For more information, you can look at this post:
What's the ASP.NET Webservice request lifecycle?
Another way to achieve what you want to do is to use the UpdatePanel. I personally don't like it, but it lets you access all the controls that are inside it, and update their values.
This control takes care of the client side update via javascript (but it actually replaces big parts of html in the page so it might be quite slow)

Calling a web method via AJAX has no impact on the HTML that has already been rendered to the browser.
What you'll need to do is return some information from your web method, and when the AJAX call completes, use jQuery to modify the appearance of the screen using the new information.
If what you're doing in the web method would result in a big change, one that you couldn't easily make happen in jQuery (such as re-rendering a GridView) you might want to look at UpdatePanels.

Related

Display busy cursor while processing queries

I've been searching for this and couldn't find something that works as I'd want it to. I have a classic asp.net webpage which makes a lot of SQL queries, some longer than others. I am not very familiar with terms to use, but the page doesn't load on each commands. So let's say the user fills some fields, then presses "Submit", it looks like nothing is happening even though SQL is processing a query.
I am looking for a way to let the user know a process is going on (busy/wait/hourglass cursor is enough for me). I tried some JavaScript calls on onbeforeunload and onunload events but as I said, page doesn't "reload" once the user did an action on it. I tried to look for the same for postbacks, but I couldn't find much and I'm not even sure postback is what is happening.
Any help with what to look for would be appreciated. As it is right now and with my limited understanding of how things work in a asp.net webpage, I'm not sure what to search for anymore.
Thanks.
You could do this if your using classic ASP.NET
Create a JS function
function doHourglass()
{
document.body.style.cursor = 'wait';
}
Add these two events on your Body Tag. If your using an update panel add them to the update panel tag instead
<body onbeforeunload="doHourglass();" onunload="doHourglass();">
http://www.codeproject.com/Articles/8243/Hourglass-cursor-for-Web-ASP-NET-pages

Interacting with web pages in C#

There is a website that was created using ColdFusion (not sure if this matters or not). I need to interact with this web site. The main things I need to do are navigate to different pages and click buttons.
I have come up with two ideas on how to do this. The first is to use the WebBrowser control. With this, I could certainly navigate pages, and click buttons (According to This).
The other way is to interact with the html directly. Not sure exactly how to do this, but I am assuming I could click buttons or use HTML requests to interact with the page.
Does anyone have a recommendation on which way is better? Is there a better way that I haven't thought of?
I'd use Html AgilityPack to parse the html and then do POSTs and GETs appropriately with HttpWebRequest.
While it may be possible to use the WebBrowser control to simulate clicks and navigation you get more control with Html AgilityPack and HttpWebRequest regarding what gets sent
Did you consider Selenium? The WebDriver API is quite good, and permits a lot of things in terms of Website automation.
why not submit directly the url? that's what the button click will do.
using WebRequest.Create you can submit directly to the url. no need to load, parse and "click" the button.
HtmlAguilityPack is useful for pulling the web elements and finding tags easily. If you need to remotely "steer" a web session, though, I prefer to use WatiN. It bills itself as a web unit testing framework, but it's very useful anytime you need to fake a browser section. Further, it can remote control different browsers well enough for most tasks you'll need (like finding a button and pushing it, or a text field and filling in text if you need a login).

How can I asynchronously add controls to a asp.net page?

I'm looking to optimize pages within our application so they perform better. I want to dynamically add controls (Textbox, Button, Custom Control, etc) to a page using AJAX to minimize postbacks done. I'm looking for an alternative to using an UpdatePanel (nothing wrong with them, they're just not built for performance).
The trick though is that the ViewState will need to be updated with the new controls added, so I can't see WebServices or static Page Method calls fitting in well here.
Ajax calls cannot update viewstate as that is created in code behind.
Maybe if you manage to access the viewstate server side you could send it back in the ajax call and store it in the viewstate field but you risk loosing anything that was not touched in the callback, I have never delved that deep into viewstate.

Form View control Ajax paging

I'm just wondering if anyone can help with this problem I have?
I have a form view and don't like the current paging that is provided by microsoft. So in the pagertemplate I am adding a button in for Next, Previous etc. Is it possible to create an ajax paging method without using the horrible update panels? Is it possible to have a pager method that gets the event args etc?
When the user clicks next it should populate the form view and two other controls on the page. So say I click next to go to page two, this will get the ID from the List, this Id should also be passed onto the other controls on the page.
Also, if anyone has any suggestions for using a better control please let me know!
If anyone has an answer, suggestion or site that could help, please let me know!
thanks all!!
Louis
How's your javascript? This kind of thing is quite trivial once you get used to writing ajax calls from javascript. I'd recommend using something like jquery as a wrapper around the ajax call. Look at the jquery documentation.
How you implement the server side depends on what framework you're using. It's very natural if you use MVC, but if you use WebForms then you probably want a .asmx webservice.
If you don't want to get into javascript then an updating form region is the kind of thing that update panels are quite good for - although I agree they are 'horrible' in that they're a bit of a lazy way of doing ajax.

Asychnronous Message Send Asp.net

I'm trying to learn about how AJAX stuff works under the hood. Say I have an asp.net form with a button on it. I then have a database with a single column DateTime. When I click the button on the asp.net form I want to insert a column in the database with the current time.
I'll have a C# event handler:
protected void btnButton_OnClick(Eventargs e, object sender)
{
//insert DateTime.now into DB
//This part is easy
}
What I would like to happen on the form itself is to have the button click NOT reload the form. It would just send a message to the server; there is no reason for the page to reload. What are some ways I could accomplish this?
I've looked into the AJAX framework a little bit and it seems like this could be done within an update panel, but the page would still reload, it would just be less noticable to the user.
Use the __doPostBack call in javascript. I have no idea how this works.
It sounds like you're partial to Microsoft's provided javascript libraries, which is fine. The UpdatePanel and the like have their place, but I've found that when you need to do something very simple and straightforward like what you're describing it is easier and cleaner to get it done with a direct call to a server resource from Javascript.
There are many ways to do this, but I like using jquery's $.ajax() method to invoke webservice methods (or MVC actions more recently). If you're willing to examine some slightly different technologies here's a blog post that will give you a taste of what I'm talking about.
ASP.NET Ajax has 2 major parts:
server side controls based
client side framework (which is called Microsoft Ajax)
With the first you would opt to make use of an UpdatePanel control. This however still sends a lot of unneeded data over the wire like the full viewstate, your pages follow almost the full page life cycle like with a normal postback. Mostly I recommend against using that in production code.
The second part is an ajax library based on pure javascript with a touch of Microsoft sauce over it. It has similarities with the page life cycle like a pageLoad function and such and is quite easy to learn.
__doPostBack
That function gets inserted when you place certain server controls on your page like a LinkButton control. You can call that from javascript directly but know that it'll cause a full page postback to the server so if you want to avoid that don't go that path.
During the last 2 years however I've become a big fan of jQuery which works quite well together with ASP.NET and ASP.NET MVC.
I suggest that you read this fine article to get more information about it: Using jQuery with ASP.NET Part 2: Making an AJAX Callback to ASP.NET.
jQuery itself has also been adopted and favored by Microsoft. I strongly suggest you take a look at it and its power.

Categories

Resources