UpdatePanel局部刷新用法
AjaxTest.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxTest.aspx.cs" Inherits="WebApplication1.AjaxTest" %> <!DOCTYPE html> <html> <head runat="server"> <title>AjaxTest Page</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <div> <asp:Label ID="Label1" runat="server" Text="国家名称:" Font-Bold="True" Font-Size="Large"></asp:Label> <asp:DropDownList ID="DropDownList0" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList0_SelectedIndexChanged"> <asp:ListItem Text="请选择" Value=""></asp:ListItem> <asp:ListItem Text="亚洲" Value="亚洲"></asp:ListItem> </asp:DropDownList> <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList> </div> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="DropDownList0" /> </Triggers> </asp:UpdatePanel> </form> </body> </html>
AjaxTest.aspx.cs代码:
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class AjaxTest : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void DropDownList0_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=localhost;uid=techtest;pwd=techtest;database=TechTest"); string sql = "select CountryName from CountryCode"; SqlDataAdapter myAdapter = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); myAdapter.Fill(ds, "CountryCode"); this.DropDownList1.DataSource = ds.Tables["CountryCode"].DefaultView; this.DropDownList1.DataValueField = "CountryName"; this.DropDownList1.DataTextField = "CountryName"; this.DropDownList1.DataBind(); } } }
页面效果: