开发者

ASP.NET - How to track event from previous postback?

I have a requirement to check for a certain condition on postback before redirecting (Response.Redirect) to another page.

Note... I cannot use JavaScript to detect whether or not to confirm (this is also a requirement) :s

Pseudo:

protected void lbtnRedirect_OnClick(object sender, EventArgs e)
{
    if (showConfirm)
    {
       // Set flag for client side
       this.ShowConfirm = true;

       // Track this event for next postback.
    }
    else
    {
       Response.Redirect("somepage.aspx");
    }
}

If the showConfrim flag == true, then the client will be show a modal dialog box asking them if they are sure they want to redirect. If the user clicks on "Yes", then the page post开发者_如何学编程s back and the desired effect is that the lbtnRedirect_OnClick event is fired. How would I about tracking the lbtnRedirect event?

Edit: I have no problem tracking the flag to show the modal (yes JS must be used to show the modal... somethings you just cannot get rid of :)). I should have been more clear.

It is when the user clicks "Yes" to continue the redirect. The page will postback again but needs to know which event to go through.

i.e. Suppose there are 3 onclick events, 1) lbtnRedirect1_Onclick 2) lbtnRedirect2_OnClick 3) lbtnRedirect3_OnClick... each of which does the confirm check.

Each onclick event does the check. So when the user clicks on "Yes" on the modal, how does the page know which event to drop back into?


You can use ViewState if you're in WebForms.

Implement a ShowConfirm property encapsulating ViewState["ShowConfirm"].

In the first postback you'll set ShowConfirm 'true', and this will activate that modal during the render (if ShowConfirm is true, that's setting as visible 'true' some control).

In the next postback, you'll set ShowConfirm 'false' because is 'true', and finally you'll do the whole redirect!


You can use an ajax call from javascript to set the required values.


Since the postback will happen before even the execution reaches to your button click event we need a workaround here, And if you don't need JS as your requirement, so take a look at

Implementing Client Callbacks Programmatically without Postbacks in ASP.NET

This is much like a wrapper for XMLHttp Ajax call IMHO.


You cannot easily create a model form, without javascipt.

One suggestion I would make is to have panels in your page.

Panel one is visible.

On submit one; panel one hides and panel two is visible asking for a confirmation.

On panel two is a confirm button, clicking this button your redirection is performed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜