2011-12-01 121 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>