【发布时间】:2015-09-27 09:56:16
【问题描述】:
我想在选择下拉项目时更改 ASP.Net 中的 URL。例如“www.website.com”,我想在 URL 中显示状态,如“www.website.com/Maharastra”。 如果我现在选择地区,我想将我的 URL 显示为“www.website.com/Maharastra/Mumbai”。
默认情况下,当页面最初加载时,我在列表视图中显示所有状态的列表,并实现了工作正常的 URL 重写概念。我想以相同的方式为下拉列表实现它。怎么做
在 Global.asax 中
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("StateRoute", "{StateName}", "~/Default.aspx", false, new RouteValueDictionary { { "StateName", String.Empty } });
routes.MapPageRoute("DistrictRoute", "{StateName}/{DistrictName}", "~/State.aspx", false, new RouteValueDictionary { { "StateName", String.Empty }, { "DistrictName", String.Empty } });
}
在 Default.aspx 中
protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.RouteData.Values["StateName"] != null)
{
dlstState.SelectedIndex = -1;
dlstState.Items.FindByText(this.Page.RouteData.Values["StateName"].ToString()).Selected = true;
dlstState_SelectedIndexChanged(null, null);
}
if (this.Page.RouteData.Values["DistrictName"] != null)
{
dlstDistrict.SelectedIndex = -1;
dlstDistrict.Items.FindByText(this.Page.RouteData.Values["DistrictName"].ToString()).Selected = true;
dlstDistrict_SelectedIndexChanged(null, null);
}
}
【问题讨论】:
-
URL 是只读的。你不能像他们这样
标签: c# asp.net url-rewriting