[Reprint]. NET ASP.NET web Form (.aspx) uses ajax to achieve local refresh

Posted by urneegrux on Tue, 15 Oct 2019 08:48:04 +0200

C# All runat="server" controls will cause the entire interface to refresh, if you want to achieve partial refresh, you can use ajax. The controls that need to be added are ScriptManager and Update Panel, which can only refresh the contents of Update Panel.
AJAX has been encapsulated in a control in C# and can be easily implemented in a way similar to panel:
Example: In the foreground aspx file:

<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager><br />
<div style="text-align: center; color: Blue; font-size: larger">
//Content that does not need refresh
</div>
<div style="text-align: center">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server"></asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
<div id="divButton" style="text-align: center"></div>
</div>
</form>
</body>

 

Among them < asp: Script Manager > is the control to implement AJAX, which needs to be added to the top of all page contents, thrown on the top, and no content is added between tags. <asp:Update Panel> ends with </asp:Update Panel>. When refreshing content in this area, the whole page will not be refreshed, and the content between tags will be refreshed locally.

Background pages can be found under AJAX Extensions in Toolbox after VS2008. In VS2005, AJAX plug-ins need to be installed to use AJAX plug-ins.
Matters needing attention:
(1) The Script Manager should be placed in front of the Update Panel.
(2) The control in UpdatePanel can only affect the control in UpdatePanel, not the control outside, while the control outside can affect the control in UpdatePanel.
Common mistakes
(1) The type "System.Web.UI.UpdatePanel" does not have a public property named "DropDownList"
Solution: The reason is simple. Just one less <ContentTemplate> </ContentTemplate>.
Example:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<asp:DropDownList ID="ddlUsed" runat="server">
<asp:ListItem Text="Baidu" Value="-1"></asp:ListItem>
<asp:ListItem Text="Google" Value="0"></asp:ListItem>
<asp:ListItem Text="Sina" Value="1"></asp:ListItem>
</asp:DropDownList>
</asp:UpdatePanel>

 

Correct as follows:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlUsed" runat="server">
<asp:ListItem Text="Baidu" Value="-1"></asp:ListItem>
<asp:ListItem Text="Google" Value="0"></asp:ListItem>
<asp:ListItem Text="Sina" Value="1"></asp:ListItem>
</asp:DropDownList> 
</ContentTemplate> 
</asp:UpdatePanel>

 

(2) Control "ScriptManager1" of type "ScriptManager" must be placed in a form tag with runat=server

Reasons for the error: <asp: Script Manager ID= "Script Manager 1" runat= "server"> </asp: Script Manager> This must be placed in the <form> tag.
--------
Copyright Statement: This article is the original article of CSDN blogger "Keep a compliment on GH when you get it". It follows CC 4.0 BY-SA Copyright Agreement. Please attach the link of origin and this statement for reproducing.
Links to the original text: https://blog.csdn.net/qq_434300/article/details/100092123

Topics: ASP.NET Google less