Link to home
Start Free TrialLog in
Avatar of mmalik15
mmalik15

asked on

how to redirect in aspx page in c#

I need to redirect a page to another page directly on the server. i.e. any request to this page
http://www.elib.scot.nhs.uk/SharedSpace/rehab/Pages/Index.aspx?ContainerID=200298
should go to http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx

I can't make change in the .cs file. I am trying to add something like this in aspx file but no luck

<%

    if (!String.IsNullOrEmpty(Request.QueryString["ContainerID"]))
        {
            var x = Request.QueryString["ContainerID"].ToString();
            if (x == "200298")
                Response.Redirect("http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx");

        }

%>
Avatar of Evan Cutler
Evan Cutler
Flag of United States of America image

Because you are working a server side push, you can try:

Server.Transfer("Webform2.aspx");

Instead of Response.Redirect.

See if that helps.
Avatar of mmalik15
mmalik15

ASKER

sorry it does not work I have tried the following

<%@ Page language="c#" Codebehind="Index.aspx.cs" AutoEventWireup="false" Inherits="nes.SharedSpace.Pages.Index" %>


<%

    if (!String.IsNullOrEmpty(Request.QueryString["ContainerID"]))
        {
            var x = Request.QueryString["ContainerID"].ToString();
            if (x == "200298")
                Server.Transfer("http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx");

        }

%>

and when I click on http://www.elib.scot.nhs.uk/SharedSpace/rehab/Pages/Index.aspx?ContainerID=200298 I get a blank page
ok...so this is inline ASP?  as in ASP Classic?

And are you looking at this as part of the page load?
or as part of a change in status to a control on the page?

One more thing:
I tried to navigate to:
http://www.elib.scot.nhs.uk/SharedSpace/rehab/Pages/Index.aspx 
without the ?= element, and that page itself does not load.

Is that page missing?
Thanks
Avatar of Christopher Kile
If you can't make a change in the .cs file of your code-behind, why make this a server-side script?

I'm going to assume you are using Javascript for your client script.  Replace your <% %> block with:

<script type="text/javascript" language="javascript">
    if ("<%=Request.QueryString["ContainerID"].ToString()%>" == "200298")
        window.navigate("http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx");
</script>

Open in new window


Alternatively, you can use

<script type="text/javascript" language="javascript">
    if ("<%=Request.QueryString["ContainerID"].ToString()%>" == "200298")
        window.location = "http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx";
</script>

Open in new window

My aspx page now have the following markup

<%@ Page language="c#" Codebehind="Index.aspx.cs" AutoEventWireup="false" Inherits="nes.SharedSpace.Pages.Index" %>

<script type="text/javascript" language="javascript">
    if ("<%=Request.QueryString["ContainerID"].ToString()%>" == "200298")
        window.navigate("http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx");
</script>

now I am getting an error on the page. Please click on the page to see the exact error

http://www.elib.scot.nhs.uk/SharedSpace/rehab/Pages/Index.aspx?ContainerID=200298
Let's change it up a bit:

<script type="text/javascript">
var first = getUrlVars()["ContainerID"];
if (first == 200298)
{
	window.location.href = 'http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx';
}

	


function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}


</script>

Open in new window

test.html
I have  changed the script but still getting the last error.

<script type="text/javascript">
var first = getUrlVars()["ContainerID"];
if (first == 200298)
{
      window.location.href = 'http://www.knowledge.scot.nhs.uk/fallsandbonehealth.aspx';
}

      


function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}


</script>
Check out the file I gave you.
I tested it, and it went to the site as requested.
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
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