Friday 13 February 2009

ASP.NET page validation and redirection to a new page

I was working on the ASP.NET web site where users register using form and get redirected to a different page on pressing submit button. Easy way to redirect to a new page is to use PostBackUr property of the button.

<asp:Button runat="server" ID="BtnSubmit"  Text="Submit" PostBackUr="newpage.aspx" />

Unfortunately this does not work if we need to handle button OnClick event. Solution is to use Server.Transfer or Response.Redirect of the Page control.

protected void BtnClick(object sender, EventArgs e)
{
        if (Page.IsValid)
        {
                // Do something ...

                Response.Redirect("newpage.aspx");
        }
}

Page.isValid is true if all validators on the page and in web controls returned valid. A very good explanation about the difference between Server.Transfer and Response.Redirect can be found in the link below.

Server.Transfer Vs. Response.Redirect

No comments:

Post a Comment