综合这几天所学到的东西,写了个小东西,算是个成果吧(注意:该例子中用到的数据库是AccessXP里的Northwind.mdb)
<%@ Import namespace="System.Data"%> <%@ Import namespace="System.Data.OleDb"%> <HTML> <body> <form runat="server"> <asp:dropdownlist id="mydls" OnSelectedIndexChanged="subListChange" AutoPostBack="True" Runat="server"></asp:dropdownlist><asp:datagrid id="mydg" runat="server" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4"> <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle> <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle> <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle> <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle> <PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle> </asp:datagrid> <br> <asp:button id="mybt" onclick="mybt_click" Runat="server" BorderStyle="Ridge" ForeColor="LightBlue" Text="Show All"></asp:button> <asp:DataGrid ID="mydg2" Runat="server" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" Visible="False" GridLines="Horizontal"> <FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle> <SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle> <AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle> <ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle> <HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle> <PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle> </asp:DataGrid></form> <script language="vb" runat="server"> sub page_load(source as object,e as eventargs) if not ispostback then dim strConnection as string dim dbName as string dbName=server.mappath("db/Northwind.mdb") strConnection="Provider=Microsoft.JET.OleDb.4.0;Data Source="&dbName dim myConnection as OleDbConnection myConnection=new OleDbConnection(strConnection) dim strSQL as string strSQL="SELECT * FROM 类别" dim myCommand as OleDbCommand myCommand=new OleDbCommand(strSQL,myConnection) myConnection.Open() mydls.DataSource=myCommand.ExecuteReader() mydls.DataTextField="类别名称" mydls.DataValueField="类别名称" mydls.DataBind() myConnection.Close() strSQL="SELECT * FROM 类别" myConnection.Open() myCommand.CommandText=strSQL mydg2.DataSource=myCommand.ExecuteReader() mydg2.DataBind() myConnection.Close() end if end sub sub subListChange(S as Object,e as EventArgs) dim strConnection as string dim dbName as string dbName=server.mappath("db/Northwind.mdb") strConnection="Provider=Microsoft.JET.OleDb.4.0;Data Source="&dbName dim myConnection as OleDbConnection myConnection=new OleDbConnection(strConnection) dim strSQL as string strSQL="SELECT * FROM 类别 WHERE 类别名称='"&mydls.SelectedItem.Value+"'" dim myCommand as OleDbCommand myCommand=new OleDbCommand(strSQL,myConnection) myConnection.Open() mydg.DataSource=myCommand.ExecuteReader() mydg.DataBind() myConnection.Close() end sub sub mybt_click(sender as object,e as eventargs) if mydg2.Visible=false then mydg2.Visible=true mybt.Text="Hide All" else mydg2.Visible=false mybt.Text="Show All" end if end sub </script> </body> </HTML>

|