在asp.net中,如何在不单击页面重定向的情况下更改按钮单击时的URL [英] How to change URL on button click without page redirect in asp.net
问题描述
我有一个类似于 此链接 的页面,列出项目.一切正常.只是我无法重写在不同级别单击的按钮上page
的URL
.
EG:
当我单击Level1 div
上的Button
手机",然后单击Level2 div
上的Button
黑莓"时,则URL
必须为"www.somesite.com/cellphone/blackberry"
,当我click on Button
手机"时我必须将URL
更改为"www.somesite.com/cellphone"
EG:
when i click on Button
"CellPhone" on Level1 div
and later on click on Button
"BlackBerry" which is on Level2 div
then URL
must be "www.somesite.com/cellphone/blackberry"
and again when i click on Button
"cellphone" i must change URL
to "www.somesite.com/cellphone"
请问有人可以建议我在asp.net.
Please can any one suggest me to do the same in asp.net.
注意: 我正在使用Visual Studio 2008 Framework 3.5
更新:
function changeState(num) {
var stateObj = { foo: "Default" };
//alert(window.history.length);
if (num == 1) {
alert("cellphone");
window.history.replaceState(stateObj, "HtmlPage", "Cellphone");
Show("divLevel2");
}
else if (num == 2) {
alert("sprint");
window.history.replaceState(stateObj, "HtmlPage", "CellPhone/Sprint");
Show("divLevel3");
}
else if (num == 3) {
alert("iphone6");
Show("divLevel3");
window.history.replaceState(stateObj, "HtmlPage","CellPhone/Sprint/iPhone6");
}
}
$(document).ready(function() {
HideAll();
});
function Show(divid) {
$("#" + divid).css("display", "block");
}
function HideAll() {
$("#divLevel2").css("display", "none");
$("#divLevel3").css("display", "none");
}
ASPX标记:
<div class="divMain" class="box">
<div id="divLevel1" class="box">
<asp:UpdatePanel runat="server" ID="updatePanelLevel1">
<Triggers>
</Triggers>
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="CellPhone" class="Button1" OnClick="Button1_Click"
ForeColor="Black" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div id="divLevel2" class="box">
<asp:UpdatePanel runat="server" ID="updatePanelLevel2">
<Triggers>
</Triggers>
<ContentTemplate>
<asp:Button ID="Button2" runat="server" Text="Sprint" class="Button2" OnClick="Button2_Click"
ForeColor="Black" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div id="divLevel3" class="box">
<asp:UpdatePanel runat="server" ID="updatePanelLevel3">
<Triggers>
</Triggers>
<ContentTemplate>
<asp:Button ID="Button3" runat="server" Text="iPhone6" class="Button3" OnClick="Button3_Click"
ForeColor="Black" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
Pagename.ASPX.CS
protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "myFunction1", "changeState(1);", true);
}
protected void Button2_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "myFunction2", "changeState(2);", true);
}
protected void Button3_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "myFunction3", "changeState(3);", true);
}
当我单击"iPhone6"按钮时,单击事件不会触发!有什么主意吗?
推荐答案
如上所述,您不能在服务器端实现此目的,而必须诉诸使用javascript.
As mentioned, you can't achieve this on the server side, but have to resort to using javascript.
You can manipulate the URL like that, without triggering actual navigation, in browsers that support history.pushState()
.
在其他浏览器中,您可以求助于URL的哈希部分,这也不会触发导航(``)
In other browsers, you can resort to manipulating the hash part of the URL, which will also not trigger navigation (``)
如果您使用的是jQuery,它可能看起来像这样:
If you're using jQuery, it could look like this:
$('#buttonId').click(function(){
if(history.pushState){
//Will become example.com/cellphone/blackberry
history.pushState(null, '', 'cellphone/blackberry');
}
else {
//Will become example.com#cellphone/blackberry
window.location.hash = 'cellphone/blackberry'
}
});
这篇关于在asp.net中,如何在不单击页面重定向的情况下更改按钮单击时的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!