[英]Update Panel not updating content
我曾嘗試過一段時間,但無法解決它。 以下是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單擊事件的代碼:
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
你能告訴我我錯過了什么嗎?
在下拉菜單中將自動回傳設置為true。
正確的含義是:每次下拉列表中的值更改時,都會發生回發到服務器的情況。
但是,請聽蒂姆·施密特的回答。 如果下拉菜單保持不變,則最好將其放置在更新面板之外,並且必須通過下拉菜單異步觸發更新面板(如果您未將觸發器設置為更新面板,則每次回發都會更新您的更新面板)。 如果下拉菜單的內容發生變化,請將其放在updatepanel中。
但是就像我說的那樣,我已經很長時間沒有使用它了,仔細檢查我對這個主題的所有說法是一個好主意。 = p
PS:Microsoft Update Panels易於開發,但是會使您的網站非常慢。 嘗試了解aspnet Web服務和jQuery。
您需要將Scriptmanager放置在將要使用它的任何控件之前。
因此,將其置於您的dropdownlist控件上方。 您還需要在下拉列表上將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>
您忘記了在DropDownList上將AutoPostback
設置為True
。 默認值為False
。
http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.listcontrol.autopostback.aspx
並且,如果用戶更改了DropDownList,如果要在UpdatePanel中觸發Async-Postback,則必須為UpdatePanel指定一個AsyncPostbackTrigger ,因為DropDown在它的外部。
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
除此之外,您還需要按照Ed所說將ScriptManager放在開頭。 在這里查看有關ASP.NET-Ajax的更多信息:
以下是正確的標記,只需將其放入您的頁面即可:
<!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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.