2010-05-26 38 views
0

如何在myDropDownList_SelectedIndexChanged()事件觸發時轉到頁面上的定位標記? 我使用常規的ASP.NET窗體。asp.net下拉列表回發到錨

更新:以下內容適用於ASP.NET按鈕。當我從下拉列表中選擇一個選項時,我想達到相同的功能(去#someAnchor)。

<asp:Button ID="btnSubmit" runat="server" Text="Do IT" Width="186px" PostBackUrl="#myAnchor" CausesValidation="false" /> 

更新:我會嘗試進一步解釋我最初沒有詳述的細節。 這是一個長頁面,在頁面中間有一個下拉列表。下拉列表下方是一個標籤,該標籤將根據從下拉列表中選擇的項目進行更改。回發期間將發生標籤更新。此時該頁面需要繼續關注下拉菜單。我試圖使用AJAX來實現這一點,但其他實現細節阻止了它的工作。回發後轉到錨標籤的能力將是一個簡單的解決方案。這是我想要完成的簡化版本。

<%@ Page Language="VB" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<script runat="server"> 

    Protected Sub myDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) 
     myLabel.Text = myDropDown.SelectedValue 
     'When this finishes, go to #myAnchor 
    End Sub 
</script> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     Imagine many lines of text here. 
     <a name="myAnchor"></a> 
     <asp:DropDownList ID="myDropDown" runat="server" OnSelectedIndexChanged="myDropDown_SelectedIndexChanged" asdf="asdf" PostBackUrl="#myAnchor"></asp:DropDownList> 
     <asp:Label ID="myLabel" runat="server"></asp:Label> 
    </div> 
    </form> 
</body> 
</html> 
+0

你是什麼意思「去一個錨標記」?跟隨錨點的href? – 2010-05-26 16:25:05

+0

我編輯了這個問題來澄清我的意圖。 – 2010-05-26 16:33:50

+0

我認爲你真的需要對你的問題更具體。在這一點上,你的問題沒有多大意義。也許你可以發佈更多的代碼。 – user153410 2010-05-26 16:40:25

回答

0

這是我已經實現了我想要的結果。

Protected Sub myDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myDropDown.SelectedIndexChanged 

    Response.Redirect("Default.aspx?myDropDown=" & myDropDown.SelectedItem.Text.ToString.Trim & "#myAnchor") 

End Sub 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    If Not IsPostBack Then 
     Dim myDropDownValue As String = Request.QueryString("myDropDown") 
     If myDropDownValue <> "" Then 

      myDropDown.Items.FindByText(myDropDownValue).Selected = True 
       Label1.Text = GetTextBasedOnDropDownSelection(myDropDownValue) 

     End If 
    End If 
End Sub 
+0

我知道這很哈克。我寧願有一個更好的解決方案... – 2010-05-27 15:34:21

0

我會用JavaScript的 - 無論是在你的代碼隱藏註冊腳本,或者有一個asp:文字是SelectedIndexChanged事件後,纔可見。修改location.href以附加您的錨點。

0

一種方法是使用ASP.NET中的forms.Controls bla bla bla屬性。然而,我建議你使用一個asp.net超鏈接控件或鏈接按鈕,這將允許你直接用它的ID訪問它。

謝謝, MNK。

3

這可能做的伎倆

<asp:DropDownList ID="DropDownList1" runat="server" 
     onchange="document.location= this.value"> 
     <asp:ListItem Text="Anchor" Value="#anchor"></asp:ListItem> 
     <asp:ListItem Text="Anchor2" Value="#anchor2"></asp:ListItem> 
    </asp:DropDownList> 

你提到myDropDownList_SelectedIndexChanged()(服務器代碼),但你必須這樣做在客戶端,除非你有一個很好的理由去服務器

+0

不幸的是,我確實需要回到服務器。 – 2010-05-26 17:24:14

0

如果你的下拉清單包含三個項目,例如說:

  • 第1頁
  • 第2頁
  • 第3頁

給這個下拉列表的AutoPostBack="true"一個屬性,然後在下拉OnSelectedIndexChanged方法寫:

if (DDl.SelectedIndex == 1) { 
    Response.Redirect("~/page1"); 
} 
else if (DDl.SelectedIndex == 2) { 
    Response.Redirect("~/page2"); 
} 
0

這個要求簡單的JavaScript solution.But問題是設計是有缺陷的。一旦移動到屏幕上的新區域,無法進入導航選擇列表而無需滾動回來。總之類似以下作品

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script type="text/javascript"> 
     function Goto(x) { 
     window.location = "#"+x.value; 
     } 

    </script> 
</head> 
<body> 
    <select id="Select1" name="D1" onchange="Goto(this);"> 
     <option value="Loc1" >go to 1 </option> 
     <option value="Loc2">go to 2 </option> 
     <option value="Loc3">go to 3 </option> 
     <option value="Loc4">go to 4 </option> 
    </select><form id="form1" runat="server"> 
    </form> 


<strong> <a href="#" id="Loc1" >Location 1</a></strong> 

blah 
    <strong><a href="#" id="Loc2">Location 2</a></strong> 

    <strong><a href="#" id="Loc3">Location 3</a></strong> 

    <strong><a href="#" id="Loc4">Location 4</a></strong> 
</body> 
</html> 
1

添加此信息到您的網頁加載,您將很好去。

Page.MaintainScrollPositionOnPostBack = true;