Thursday, March 29, 2012

setting parameter for redirect

I have the following code in my page Load for my index.aspx page:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string pointer = HttpContext.Current.Request.Url.AbsoluteUri.Remove(0, 7);
if (!pointer.StartsWith("state-solutions"))
{
Response.Redirect("http://state-solutions.compasslearning.com/statedetails.aspx?id=" + pointer.Substring(0, 2));
}
else
{
state.DataSource = StateDAO.GetStateList();
state.DataValueField = "PK";
state.DataTextField = "name";
state.DataBind();
state.Items.Insert(0, new ListItem("-- Please Select --", ""));
}
}
}

As the code is currently, when I run my application in my local development environmnent, the page always goes to my searachdetails.aspx page with the querystring value ?id=lo for localhost. Whereas, I'd like it to open to my index.aspx page without have to constantly change the value for the pointer variable.

I'd like to fix the hard-coded value for the domain so I don't have to change it when moving files from my development environment, to the testing environment, to production. Also, if the domain name ever changed, I'd have to manually make the change. I was trying to use ResolveURL and then Request.ServerVariables("http_host"), but I couldn't get either to work?

I was one method I was trying:


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string pointer = HttpContext.Current.Request.Url.AbsoluteUri.Remove(0, 7);
string httphost = Request.ServerVariables("http_host");

if (!pointer.StartsWith("state-solutions"))
{
Response.Redirect("http:///" + httphost + "//statedetails.aspx?id=" + pointer.Substring(0, 2));
}
else
{
state.DataSource = StateDAO.GetStateList();
state.DataValueField = "PK";
state.DataTextField = "name";
state.DataBind();
state.Items.Insert(0, new ListItem("-- Please Select --", ""));
}
}
}

Anyone know how I can get this to worK?
Any help is appreciated.
Thanks.

simply use

Response.Redirect("statedetails.aspx?id=" + pointer.Substring(0,2));

HC

0 comments:

Post a Comment