Link to home
Start Free TrialLog in
Avatar of jjrr007
jjrr007

asked on

Change Window- Button Clicked

I wanted to please ask how can I navigate to a new page when a button is clicked.  In other words, when a button is clicked I want to navigate to new page (without opening a new window).  I prefer the simplest way to do this.  I am using ASP.NET and C# with Visual Studio 2005. I prefer to just use asp.net.  Thanks!
Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

in the code behind for the button you can do the following

Response.Redirect("mypage.aspx");
Another method, is to transfer the response object. (Review the differences between Server.Transfer and Response.Redirect)

In the code behind for the button you would put the following.

Server.Transfer("mypage.aspx");
Avatar of jjrr007
jjrr007

ASKER

Thanks for your time.  I am getting a syntax error with the code listed in the snippet.  How should i change it?  Also what is the difference between the two ways you had it.

What I want to do is have a search box on the website. When the user types in something and presses the button, I want them to be directed to  a page where they can see the search results.  Based on that, which way would you recommend.
        protected void Submit1_Click(object sender, GridViewSelectEventArgs e);
    {
        Server.Transfer("PageResults.aspx");
 
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jjrr007

ASKER

I am not getting an error message, but the page is not changing.  Below is the updated code.  Maybe I am not calling the function correctly.  what do you suggest?
         protected void Submit1_Click(object sender, EventArgs e)
    {
        Response.Redirect("NewPage.aspx");
 
    }

Open in new window

set a break point on the redirect line and make sure it is getting there.

Also, make sure that the Click event of the button is pointing at your submit1_click() method.
Where is your button placed? First you have posted a submit function and then you post a different function for same button.
Avatar of jjrr007

ASKER

Thanks for your responses.  The button is placed near the top of the ASP.net page.  

When I double clicked on the button, I am not directed to the C# code for the button.  I can't tell for certain if the button is associated with the code properly.  I had to make it up on my own. I think the last C# code seems better, but I don't know if it's right.  The button's ID is Submit1.  So based on my most recent code, do you think it is accurate?

I can try the breakpoint when I get in the office in the morning. I can let you know what happens. Thanks again!
When in design mode, select the button. Press "F4" to display the properties for the button.
Select the little lightning bolt to show the event side of the properties.
Select the correct event for the click event.
and voila, you should hopefully be set.

Avatar of jjrr007

ASKER

I didn't see the lightning bolt when I first tried.  I was using a different type of button. I have changed it to a regular button and it worked. Thanks for your time and patience with me.