How to review all datas in one pages in asp.net? - asp.net

I have five pages in asp.net web application. I entered information in first page and click next. it goes to next page and enter information in this page. I continue this process for five pages. Before click Submit button in fifth page, I need to review once all information entered by me in all previous pages under review button click. How do this? pls Advice.

You can store your data in the database, as you move along the steps, and retrieve it in the final step. You can also store it in Session, as an alternative.
Surely there are other ways to implement this scenario, but the idea is to somehow persist the data and retrieve it in the final step.

I personally think that you are going the wrong way about this. Passing data from page to page is going to cause you all sorts of trouble.
There are a number of methods to achieve this on a single page. You could create 5 seperate controls and place them all on the single page (if they aren't all too big and slow down the page load), then make the appropriate control visible depending on which the user is to see. For example, when you click next make the current control visible property false and then display the next one. Then at the end of the process when you click complete you can access the entered data from each of the controls, you could create a fancy function in the controls that return you a populated class or even pass by reference (we won't go in to that here).
You could also look in to using Views. Which is another method that you could implement on a singular page.
If you're adamant with using multiple pages then you could temporarily store the data in a database and then on the final page get the IDs from a Session and store it all finally as one. But I wouldn't advise this, although it depends on the situation. If you want them to be able to fill half the form and then continue at a later date then the database method would be advisable, but if you only want data being stored upon completion then you will end up with a lot of half completed redundant data using this method.

The solution totally depends on the framework and methods you are using. I'll try and help you with an ASP.Net MVC3 solution, as I think MVC3 is really the way to go for when using ASP.Net.
When the user clicks on the 'next' button, just load a partial view (containing the next step html) with the JavaScript / jQuery Ajax method, OR just present the next step by using using the jQuery show / hide functions.
Keep your form data within one html form-tag (or at least one holder-div).
When you need to submit the information at the last step, simply collect the information with a single jQuery serialize call and send it to your ASP.Net receiver.
Some code snippets:
Display a partial view from the Razor view-engine:
#Html.Partial("/Views/Instance/_General.cshtml", Model)
Load a partial view through jQuery - for more details, please see http://api.jquery.com/category/ajax/
#Html.Partial("/Views/Instance/_General.cshtml", Model)
Post your data to the server though jQuery:
$("#submit-button-id").click(function () {
var data = $("#form-id").serialize();
$.ajax({
url: "/Example/Save",
type: "POST",
data: data,
cache: false,
success: function (success) {
if (success) {
// things worked out just fine..
}
else {
// present some kind of error..
}
}
});
});

Related

Cross-page posting with target _blank leaves source page unfunctional

I'm trying to describe it in as few steps as possible:
I have Page1.aspx with lot of controls, and Preview and Save button among those. I also have Page2.aspx that is the redirection target of a Preview Button click.
Since I need all the controls selections from Page1 to draw a preview on Page2 the redirection is done with setting Preview's PostBackUrl.
I also must have preview shown on a new tab or window so I used onClientClick="aspnetForm.target='_blank'" for Preview button definition.
Save button-click callback, after storing data to a database does redirection to some Page0.aspx (initial list of reports - the subject of the code)
Preview button works fine - a preview renders in a new tab, but when I go to the old tab and click on Save, I see from debugger, that firstly Page2.aspx(?) and secondly Page1.aspx are loaded. Then all the data is stored in the db, but though Page0 redirection is executed Page1.aspx stays loaded in the browser.
I have no idea what processes are behind this. Could one who knows give me an insight? Or if you consider my approach impossible to implement give some idea how to do the same?
If it's of importance, everything on the Page1 is located in an update panel.
Thank you very much for replying
In ASP.NET there are basically zero (0) circumstances in which you will ever send form data from one page to another. Although what exactly you are trying to accomplish is vague, you can consider some of the following:
Isolate unique operations/systems to a single page. If you have something like a User Profile, don't have three different aspx pages; just use a single page for the user or admin to manage that data / functions. Postback events are your friend.
Understand the difference between ViewState and traditional form data. I'm guessing that if you're trying to post form data from one page to another, you probably don't understand the point of ViewState. Using a single page to maintain temporary data that the user is currently working with is a great use for ViewState. If you want the data to appear on another page then you need to consider the data from the previous page as final and thus should be saved to a database or some other medium.
These are just some general guidelines because there is no exact answer to your problem without saying something generic like "You're doing it wrong." I would recommend starting by never again trying to post form data from one aspx page to another.

Validation using navigation

I have some pages in my website and a left menu control. Control helps to navigate from one page to another.
My query is -> While user try to navigate to another page, I want to impose some validation like in the current page if the form is not saved, user will be asked to save it by using a confirm messagebox and if user presses no button, then user will be allowed to navigate otherwise, system will first save the details and then navigate.
Edit - My page is a content page, I meant, this is using a master page.
Use the following steps
window.onbeforeunload = confirmExit;
and a function that stops/continue the page execution.
function confirmExit() {
var email= document.getElementById("email");
if (email.value != "")
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
The way I would do this is to have an onbeforeunload javascript event fire which gives the user the choice to save the form. I personally would also poll the form saving data back whist they are completing it. I think this is the method SO uses.
There is a pretty decent example over on Code Project that may help http://www.codeproject.com/KB/aspnet/AutoSaveFormData.aspx
EDIT:
If you only want to call the save method you can mark it with the [WebMethod] filter and call it using XmlHttpRequest or jQuery's $.post

Go back to a search page without losing search criteria

I need some help on this problem. It is about ASP.NET MVC3. I have a page with at the top some search criteria and at the bottom the resulting data. The user can type some criteria and use a submit button to retieve data. In my controller, I have an ActionResult function for managing these criteria and return to the same page with a ViewModel class filled.
The problem: the user can click on a line in the resulting table for viewing a detail page. On the detail page, he can navigate to an edit page for editing data. On this page (edit data) I would like to able the user to go back to the search result page (in fact: navigate back two times). What is the best way to proceed? If I "simply" use an ActionLink (without posting data) to my search result page, it will simply display an empty result page with empty search criteria. Maybe I need to keep my search criteria in a session variable? I don't like this kind of thing...
Any help will be highly appreciated.
Why not place the data in the Session, as you say?
public ActionResult Search(string searchCriteria)
{
Session["searchCriteria"] = searchCriteria;
// do more stuff
}
This way you have the search criteria available no matter how many "back clicks" the user does.
You could make it much more complicated but I do not think it is necessary in this case. If you want to pass it as route data in an action link you'll have to defensively add a searchCriteria parameter to every ActionLink of the pages the user might navigate to from the Search page. That makes it a lot more cumbersome in my opinion.
Good enough is sometimes good enough. Refactor later as needed. :)

JQuery and ASP.NET Web Forms

I am creating a ASP.NET Web Form application where I am trying to use some nice jQuery functionality and flashyness. The current part of the application I have consists of two jQuery UI tabs; a search tab and a results tab. When the user performs a search from the search page, the results tab will be selected and the results will be displayed in this tab. I need to get the results into a gridview. Now this is where the issue starts to come in:
The easiest way to get the search results is to allow the search click to perform the postback where I can then format the datasource with the parameters from the input fields and let the datagrid take care of itself and data bind and show the results. The thing is, this really doesn't look that great (due to the whole post back and such) as well as starts to cause some issues with using javascript/jQuery to take care of tab switching and all that portion because the postback reinitializes everything from the jQuery UI (i.e. the jQuery UI tabs). So in short, the postback allows for easy binding of the input for the search and getting the results, but makes the page and its behavior all wonky.
I was wondering if there is a standard way to do this type of mixing jQuery/javascript/AJAX all together within web formto get the functionality of things like the gridview and such. I am wondering if there are some good tutorials, or even just a direction on solving this issue.
I hope all this made sense, and thank you all for your help.
I don't think this is a standard, but here is the pattern I use:
First of all, I use Page Methods for ASP.Net to get hooked back up to the server. In this case it would be something like this:
PageMethods.Search(searchValue, onSearchComplete);
That calls a static page method in the page, like this:
public static void Search(string searchValue)
...
Inside that procedure, I create an instance of a user control which contains the gridview, and invoke a method on that control, passing the searchValue:
var searchControl = (SearchControl)new SearchControl().LoadControl("/controls/SearchControl.ascx");
searchControl.Search();
var stringBuilder = new StringBuilder();
using (var textWriter = new StringWriter(stringBuilder))
{
var htmlWriter = new HtmlTextWriter(textWriter);
searchControl.RenderControl(htmlWriter);
return stringBuilder.ToString();
}
This is all going to end up as the result argument to the handler you specified in the initial call (onSearchComplete) in this example. You can do whatever you want with the markup, including slapping it into a div, or alerting it for debugging.

Pass a value and reload User Control from Javascript

I have a User control (because I use the same in other page, so I thought I should reuse code and not double my work), but in this page I show a list of companies and each one has a company number, I need to pass this company number to that User Control and it has to reload using that passed company number.
How can I accomplish this?
what I have so far:
alt text http://www.balexandre.com/temp/2009-09-17_0917.png
the Show company structure link is made of
<a href="javascript:showStruct('112:201334607','5564967221');"
class="showStructLink">Show company structure</a>
the showStruct method is written like
function showStruct(pid, cnr) {
if (_showStrut == 0)
return;
// fancy stuff to be more apealing visually
$("#tdSearch").removeClass("tabTitleUp01").addClass("tabTitleDownUp01");
$("#tdStruct").removeClass("tabTitleDownUp02").addClass("tabTitleUp02");
$("#srtr1").hide();
$("#srtr2").hide();
$("#sttr1").show();
// enable Search Results tab to be clicked in order to get back
$("#tdSearch")
.addClass("pointer")
.bind("click", function() { hideStructure(); });
// pass the company number and reload wcCompanyStruture web user control
// __doPostBack('RefreshWebUserControl', cnr);
}
I can make a simple aspx page with the control inside and from jQuery invoke $.get() to run and populate the control correctly, but I really want to learn how to do this properly, using the ASP.NET AJAX Method to send a number and call RefreshData on it
using code-behind it is easy to refresh the user control, just invoking
wcCompanyStruture.RefreshData("companyNumberHere");
what do I need to do in my User Control side and well in the showStruct method to create this behavior?
All help is appreciated, Thank you.
I know this is not the answer to you question but I think you may be asking the wrong question.
It looks to me as if you have a search result+details view scenario that you are going about the wrong way.
When you click "Show Company structure" you want to see the details on the second tab right? If this is the case then the tab approach would be confusing to the user, it would be better with a modal popup that shows the details. No postback just AJAX load a page with the details into a modal popup window.
This is very easy with JQuery using the dialog widget in JQueryUI and the AJAX load function $('#SomeDiv').load('details.aspx?id='+companyid);
http://docs.jquery.com/Ajax/load#urldatacallback
It would give a much better user experience and it is surprisingly simple to code.
I hope this helps.
You can use a LinkButton for each "Show Company Structure" link, and set the CommandArgument property with the corresponding company id. The LinkButton will cause a postback.
A second solution would be to use a hidden variable : <input type="hidden" id="hiddenCompanyNumber"> and set it's value in the showStruct method. You can then call __doPostBack(), for which you need a control upon which to postback I think.
All in all, I think the first solution is less hacky.
You can find it here
http://codeclimber.net.nz/archive/2007/06/26/how-to-refresh-an-updatepanel-from-javascript.aspx
don't worry about the article title it has what you need Just do the four steps and you are ready to go.

Resources