开发者

Redirect without post back

I have a user registration form. Here I have link to another page (card info page) which has to be filled for the registration. User fills the few fields in the user registration form and click on the link that takes to card info page. When user clicks the link in card info page to navigate back to registration page, the previous details entered in registration got vanished. So I need to redirect from card info page to registration page without postback.开发者_如何转开发 How can i accomplish that?

Response.Redirect() is used for redirection.


You can't do this without a postback I don't think. I'd recommend storing the details from your registration page in session state then if the user returns to that page, re-populate the fields from session state.

//eg in registration page
protected void CardInfoLink_Click(object sender, EventArgs e)
{
    //store details entered
    Session["Registered"] = true;
    Session["Username"] = txtUserName.Text; 
    //etc for any other fields
    Response.Redirect("~/CardDetailsPage.aspx");
}

then in the Page_Load method you could, if the session data exists, pre-populate the form from session. e.g

if (!Page.IsPostback && (bool)Session["Registered"])
{
    txtUserName.Text = (string)Session["Username"];
    //repopulate other fields as necessary
}


When you redirect to another page it will lose all the context of that first page unless you do something to stop it. The options that spring to mind are:

  1. Create the card info page in a popup window. This will mean that your main window is unchanged in the background. You'd preferably use purely client side code to do this but if you need server side code to do it its still possible, just slightly more fiddly.

  2. Save the information on postback before redirect. This could either be just in session or in a database or you could even do it clientside in cookies if you want. Then when you revisit the page you can check if you have saved information and load it up automatically.


If you redirect the user to another page all captured info on that screen WILL be lost. View-state is not kept on redirects, but only on post-backs / callbacks

The only way to maintain information across redirects is to make use of Session Variables, Cookies, or even persisting the data to a Database / XML file and repopulate on return to that page.

I would suggest you save your info as the user gets directed to the info card, then on return, check for the values and re-populate it.


You can store the values in ViewState/Session and redirects to another page (card info page) and then re-populate the values when returning to registration page. Or showing pop-ups or panels (show/hide using Visible property) in the same page you can retain the user inputs. (If you are used server side controls the values are not cleared).


Server.Transfer() will perform a transfer to another page on the server-side.

Update: it would be possible to populate the current pages Context.Items property with the state originally being transferred by query string. This behaves similarly to session state but is limited to the current request.

Update 2: the Server.Transfer() method has an overload that accepts a bool parameter preserveForm, this preserves query string parameters:

http://msdn.microsoft.com/en-us/library/aa332847(v=VS.71).aspx


You can use any kind of ajax request on "go to the next page" button click to copy the registration data into session. Then after the returning you can populate the data again and to remove the session. Your code should be similar to this one:

----------------jquery ajax request-----------------------

function SetValuesIntoSession(value1, value2, value3) {        
        $.ajax(
        {
            type: "POST",
            url: WebServicePathAndName.asmx/InsertIntoSessionMethodName",
            contentType: "application/json; charset=utf-8",
            data: "{value1:'" + value1 + "', value2:'" + value2 + "', value3:'" + value3 + "'}",
            dataType: "json",
            success: function(response) {
                if (response.d == "Yes") {
                    //do something in correct response
                }
                if (response.d == "No") {
                    //do something for incorrect response
                }
            },
            error: function(xhr) {
                alert('Error!  Status = ' + xhr.status);
            }
        });       
}

below is the code for the web service, that should insert the data into the session. Note, that you must set "EnableSession = true" if you want to use session state in your web service

---------------------WebServicePathAndName.asmx------------------

[WebMethod( EnableSession = true )] 
public void InsertIntoSessionMethodName( string value1, string value2, string value3 )
{

    Session[ "value1" ] = value1;
    Session[ "value2" ] = value2;
    Session[ "value2" ] = value3;
}

I think, that the rest of the code should be easy to be implemented.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜