简体   繁体   中英

ASP.NET Button click redirect to new page

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

You need to handle Click event of oButton.

Button oButton = new Button();
 oButton.Text = "NextPage";
 oButton.Click += (sa, ea) =>
     {
         Response.Redirect("Redirect.aspx");
   };

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

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();
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM