Saturday, March 31, 2012

Simple - Open Page in C# ASP .net

I'm new to .net and am having a real hard time doing something that should be relatively simple. All I need to do is open a new page (url) using a button instead of a hyperlink. That's it.

protected void Button1_Click(object sender, EventArgs e)
{

//what needs to go here open this site in same window and no changes to window size, status bar, etc.?

Somefunction("http://www.microsoft.com");
}


I'm using C# on ASP .net 2.0

Response.Redirect("http://www.microsoft.com");


Nope, didn't do a thing - just refreshed teh page. Here's my full code.

<%@. Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("http://www.microsoft.com");
}
</script
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<p> </p>
<p> </p>
<form method="post" name="form1" id="form1" runat="server">
<p> </p>
<p></p>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<p>
<asp:Button ID="Button1" Text="Button" CommandName="Button1_Click" runat="server" />
</p>
</form>
<p> </p>
</body>
</html>


if you want to open the link in a new page then you have to use client side script (javascript)

open your aspx file in the code view

add OnClientClick attribute to your button and use the window.open javascript to open the new window as follows

<asp:ButtonID="btn"runat="server"Text="MSFT"OnClientClick="window.open('http://www.microsoft.com');"/>

alright, I have just re- read your post and figured out that you just need to redirect to another page within the same window,

then you have to use response.redirect as follows:

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"> protected void btn_Click(object sender, EventArgs e) { Response.Redirect("http://www.microsoft.com"); }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <asp:Button ID="btn" runat="server" Text="Button" OnClick="btn_Click" /> </div> </form></body></html>

notice that i'm using the OnClick attribute.


You lost me. I'm not new to software development - just .ASP. Why on earth would I need to switch languages just to go from one page to the next? So if I have two pages - both running under the same IIS server and I want to go from Page1 to Page2 using a button - I need to use JavaScript?? (my assumption is the process for connecting to external sites is the same as going from one page to the next within an IIS server)

Tell me you are kidding - or you mis-understood my question?


atlasp:

Nope, didn't do a thing - just refreshed teh page.

That's because you have wired up the event incorrectly.

CommandName="Button1_Click"

should be:

Onlick="Button1_Click"


atlasp:

You lost me. I'm not new to software development - just .ASP. Why on earth would I need to switch languages just to go from one page to the next? So if I have two pages - both running under the same IIS server and I want to go from Page1 to Page2 using a button - I need to use JavaScript?? (my assumption is the process for connecting to external sites is the same as going from one page to the next within an IIS server)

Tell me you are kidding - or you mis-understood my question?

Your question has been mis-understood. You do not need to use javascript.


atlasp:

Tell me you are kidding - or you mis-understood my question?

indeed, i did misunderstood you. I corrected my post.

I was misled by this sentence:

atlasp:

All I need to do is open a new page

0 comments:

Post a Comment