Hi CodeLava,
You can use Server.Transfer. But make sure you have specified the page extension (.aspx) in the url.
Check the below example.
HTML
Default
<h1>Default</h1><hr />
<a href='<%=ResolveUrl("~/Customers") %>'>View Customers</a>
Customers
<h1>Customers</h1><hr />
<asp:DataList ID="GridView1" runat="server" RepeatDirection="Vertical">
<ItemTemplate>
<table>
<tr>
<th colspan="2">
<b><%# Eval("Name") %></b>
</th>
</tr>
<tr>
<td colspan="2">
<%# Eval("Id") %><br />
<%# Eval("Country")%><br />
<asp:Button runat="server" CommandArgument='<%# Eval("ID")%>' OnClick="OnView" Text="View"></asp:Button>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Home
<h1>Customer Details</h1>
<hr />
<b>Customer Id:</b>
<asp:Label ID="lblCustomerId" runat="server" />
Code
Customers
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[3] {
new System.Data.DataColumn("Id", typeof(int)),
new System.Data.DataColumn("Name", typeof(string)),
new System.Data.DataColumn("Country",typeof(string))
});
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnView(object sender, EventArgs e)
{
//Response.Redirect("Customers/" + ((Button)sender).CommandArgument);
Server.Transfer("Home.aspx?id=" + ((Button)sender).CommandArgument);
}
Home
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
lblCustomerId.Text = Request.QueryString["id"];
}
}
Screenshot