我有一个与 this link 类似的页面列出项目。一切正常。只是我无法重写点击不同级别的URL
按钮的page
。
EG:
当我点击Button
上的Level1 div
“CellPhone”以及稍后点击Button
上的Level2 div
“BlackBerry”时,URL
必须"www.somesite.com/cellphone/blackberry"
当我click on Button
“手机”时,我必须将URL
更改为"www.somesite.com/cellphone"
任何人都可以建议我在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 MARKUP:
<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”按钮时,点击事件不会触发!任何想法?
答案 0 :(得分:1)
如前所述,您无法在服务器端实现此目的,但必须使用javascript。
您可以在支持history.pushState()
的浏览器中操纵这样的网址,而不会触发实际导航。
在其他浏览器中,你可以诉诸操纵URL的哈希部分,这也不会触发导航(``)
如果您正在使用jQuery,它可能如下所示:
$('#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'
}
});
答案 1 :(得分:0)
尝试以下代码
`
$('#<%= ButtonID.ClientID %>').click(function () {
window.location.hash = "blckberry"; // Change URL with any TEXT************
});
`
或者您也可以使用它。
window.history.pushState('obj', 'newtitle', '/blckberry');