2011-12-01 119 views
5

我一直在嘗試這個,但我無法繞過它。以下是aspx頁面顯示的代碼:更新面板沒有更新內容

<!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> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    Test<br /> 
    <asp:DropDownList ID="DropDownList1" runat="server"> 
     <asp:ListItem>1</asp:ListItem> 
     <asp:ListItem>2</asp:ListItem> 
    </asp:DropDownList> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"> 
    </asp:ScriptManager> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
     <ContentTemplate> 
      <br /> 
      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
    </ContentTemplate> 
    </asp:UpdatePanel> 
    </form> 
</body> 
</html> 

以下爲Button1的Click事件的代碼:

Public Class WebForm1 
    Inherits System.Web.UI.Page 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

    End Sub 

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DropDownList1.SelectedIndexChanged 
     Label1.Text = DropDownList1.SelectedIndex 

     UpdatePanel1.Update() 

    End Sub 
End Class 

你能告訴我什麼,我錯過了。

回答

2

在下拉列表中將autopostback設置爲true。

正確的含義是:每次下拉式菜單中的值發生變化時,都會發送回服務器。

但聽Tim Tim Schmelter的答案。如果下拉菜單不會更改,最好將其放在更新面板之外,並且更新面板必須通過下拉菜單異步觸發(如果您沒有爲更新面板設置觸發器,則每次回發都會更新您的更新UpdatePanel中)。如果下拉列表的內容發生更改,請將其放入更新面板中。

但正如我所說,我很久沒有使用它了,它是一個很好的主意,可以仔細檢查我所說的關於該主題的所有內容。 = p

PS:微軟更新面板很容易開發,但使您的網站非常緩慢。嘗試瞭解aspnet webservices和jQuery。

+0

這個問題解決了。但是,你能告訴我什麼是autopostback嗎? – surpavan

+0

非常感謝。這很有幫助。 – surpavan

1

您需要將Scriptmanager放置在任何將使用它的控件之前。

因此,將它放在您的下拉列表控件上方。 您還需要將AutoPostBack屬性設置爲true,才能在selectedindexchange事件的下拉列表上對其進行觸發。

<body> 
    <form id="form1" runat="server"> 
    <asp:ScriptManager ID="ScriptManager1" runat="server" /> 

    Test<br /> 
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostback="true"> 
     <asp:ListItem>1</asp:ListItem> 
     <asp:ListItem>2</asp:ListItem> 
    </asp:DropDownList> 

    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
     <ContentTemplate> 
      <br /> 
      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
    </ContentTemplate> 
    </asp:UpdatePanel> 
    </form> 
</body> 
+0

非常感謝。 – surpavan

1

你忘記設置AutoPostbackTrue您的DropDownList。默認值是False

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.autopostback.aspx

如果要觸發異步回發在你的UpdatePanel如果用戶更改DropDownList的,你必須指定的UpdatePanel的AsyncPostbackTrigger,由於下拉列表是在它之外。

<Triggers> 
    <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" /> 
</Triggers> 

除此之外,你需要放置ScriptManager的開頭爲Ed said。 在這裏尋找更多的相關信息在ASP.NET的Ajax:

http://msdn.microsoft.com/en-us/magazine/cc163354.aspx

+0

非常感謝。 – surpavan

1

下面是正確的標記,只是把它放到你的頁面:

<!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> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    Test<br /> 
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostback ="True"> 
     <asp:ListItem>1</asp:ListItem> 
     <asp:ListItem>2</asp:ListItem> 
    </asp:DropDownList> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"> 
    <Triggers> 
     <asp:AsyncPostBackTrigger ControlID="DropDownList1" 
       EventName="SelectedIndexChanged" /> 
    </Triggers> 
    </asp:ScriptManager> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
    <ContentTemplate> 
     <br /> 
     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
</ContentTemplate> 
</asp:UpdatePanel> 
</form>