Microsoft AJAX + SQLDataSource + DropDownList (SelectedIndexChanged) 使用方法
Default.aspx |
<form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" AppendDataBoundItems="True"> <asp:ListItem Selected="True" Text="" Value="0"></asp:ListItem> </asp:DropDownList> <br /><br /> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:SqlDataSource ID="SqlDataSource1" runat="server"> </asp:SqlDataSource> <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"></asp:GridView> </ContentTemplate> </asp:UpdatePanel> </div> </form> |
Default.aspx.vb |
Imports System.Data.SqlClient Partial Public Class _Default Inherits System.Web.UI.Page Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init GetDropList() GetTable() End Sub Private Sub GetTable() Dim AAA As New StringBuilder SqlDataSource1.ConnectionString = "Data Source=.\SqlExpress; Initial Catalog=Soccer; Integrated Security=SSPI" AAA.Append("SELECT dbo.Team.Team_Name AS 'Team Name', dbo.League.League_Name AS 'League Name' FROM dbo.Team ") AAA.Append("INNER JOIN dbo.League ON dbo.Team.League_ID=dbo.League.League_ID ") If Not (DropDownList1.SelectedValue = "20") Then AAA.Append("WHERE dbo.Team.League_ID = " & DropDownList1.SelectedValue) End If AAA.Append(" ORDER BY dbo.League.League_Name, dbo.Team.Team_Name;") SqlDataSource1.SelectCommand = AAA.ToString End Sub Private Sub GetDropList() Dim connectionString1 As String = "Data Source=.\SqlExpress; Initial Catalog=Soccer; Integrated Security=SSPI" Dim con1 As SqlConnection = New SqlConnection(connectionString1) con1.Open() Dim sb As New StringBuilder sb.Append("SELECT * FROM dbo.League ORDER BY dbo.League.League_Name;") Dim Adapter As New SqlDataAdapter(sb.ToString(), con1) Dim ds As New DataSet Adapter.Fill(ds) DropDownList1.DataSource = ds.Tables(0).DefaultView DropDownList1.DataTextField = "League_Name" DropDownList1.DataValueField = "League_ID" DropDownList1.DataBind() con1.Close() con1.Dispose() End Sub Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, _ ByVal e As EventArgs) Handles DropDownList1.SelectedIndexChanged GetTable() End Sub End Class |