Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Button click redirect to new page

Tags:

c#

asp.net

I have created a Button programmatically. I now want to redirect to a new aspx page on the click of this Button, such that we will enter into one of the methods of the new page (like page_load(), etc.)

Is this possible?

Eg:

Button oButton = new Button;
oButton.Text = "NextPage";
// Redirect to "Redirect.aspx" on click

But I am not able to find an entry point in Redirect.aspx where I can do some changes to the UI components once we get redirected to "Redirect.aspx"

Regards,

Parag

like image 899
Parag Avatar asked Aug 18 '11 05:08

Parag


3 Answers

You need to handle Click event of oButton.

Button oButton = new Button();
 oButton.Text = "NextPage";
 oButton.Click += (sa, ea) =>
     {
         Response.Redirect("Redirect.aspx");
   };
like image 151
KV Prajapati Avatar answered Nov 10 '22 09:11

KV Prajapati


You can use query string parameters and depending on the value of param call the appropriate method in Page_Load of Redirect.aspx e.g.

Redirect.aspx?val=1

in Redirect.aspx

protected void Page_Load(...){
string var = Request.QueryString["val"];
if(var == "1")
some_method();
else
some_other_method();
}
like image 38
Waqas Avatar answered Nov 10 '22 10:11

Waqas


You could also use the PostBackUrl property of the Button - this will post to the page specified, and if you need to access items from the previous page you can use the PreviousPage property:

Button oButton = new Button;
oButton.Text = "NextPage";
oButton.PostBackUrl = "Redirect.aspx";

Then in you wanted to get the value from, say, a TextBox on the previous page with an id of txtMyInput, you could do this (very simple example to give you an idea):

void Page_Load(object sender, EventArgs e)
{

   string myInputText = ((TextBox)PreviousPage.FindControl("txtMyInput")).Text;

   // Do something with/based on the value.

}

Just another example of how to accomplish what I think you're asking.

See Button.PostBackUrl Property for more info.

like image 2
Tim Avatar answered Nov 10 '22 11:11

Tim