<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.ascx" %>
<%@ Register TagPrefix="AllButton" TagName="Butn" Src="btn.ascx" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web" %>
<html>
<head>
	<script language="c#" runat="server">
	SqlConnection Source;
	SqlConnection Source1;
	void Page_load(Object sender, EventArgs e){
		AllTables.ToolTip="AllTables";
		Source = (SqlConnection)Session["NowSTKSQL"];
		Source1 = (SqlConnection)Session["NowMaster"];
		if(IsPostBack==false){
			DataSet DS = new DataSet();
			SqlDataAdapter NComm = new SqlDataAdapter("USE stockSQL EXEC dbo.sp_tables", Source1);
			NComm.Fill(DS,"AllTable");
			DS.Tables["AllTable"].DefaultView.RowFilter = "TABLE_TYPE NOT LIKE 'SYSTEM TABLE'";
			AllTables.DataSource=DS.Tables["AllTable"].DefaultView;
			AllTables.DataValueField="TABLE_NAME";//将数据表名称置于DropDownlist
			AllTables.DataBind();
		}
	}
	void selectTable(Object sender, EventArgs e){
		SqlDataAdapter NComm = new SqlDataAdapter("select * from " + AllTables.SelectedItem.Text, Source);
		DataSet DS = new DataSet();
		ArrayList AllFields = new ArrayList();
		NComm.Fill(DS,AllTables.SelectedItem.Text);
		foreach(DataColumn Col in DS.Tables[AllTables.SelectedItem.Text].Columns){
			AllFields.Add (Col.ColumnName);//加入列名
		}
		SourceFd.DataSource=AllFields;
		SourceFd.DataBind();
		TargetFd.DataSource="";//清除目的ListBox的项目
		TargetFd.DataBind();
	}
	void GetRecord(Object sender, EventArgs e){
		Cus.Visible=false;
		string QS="";
		foreach(ListItem CItem in TargetFd.Items){
			QS+= "," + CItem.Text;
		}
		if(QS==""){
			nn.Text="未选择列";
			return;
		}
		else{
			QS=QS.Remove(0,1);
		}
		QS="select " + QS + " From " + AllTables.SelectedItem.Text;
		DataSet DS = new DataSet();
		SqlDataAdapter NComm = new SqlDataAdapter(QS, Source);
		NComm.Fill(DS, AllTables.SelectedItem.Text);
		Cus.DataSource=DS.Tables[AllTables.SelectedItem.Text].DefaultView;
		Cus.DataBind();
		Cus.Visible=true;
	}
	</script>
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form runat="server">
查看 <asp:hyperlink id="h1" NavigateUrl="/aspnetdata/shows.aspx?cate=c&file=ch12/12-2/cs/btn.ascx" Text="btn.ascx" runat="server" target="_blank"/><br>
<table align="center"><tr><td align="left" colspan="3">选择数据表：<asp:dropdownlist id="AllTables" Autopostback="True" OnSelectedIndexChanged="selectTable" Runat="server" /></td></tr>
<tr><td><asp:ListBox Runat="server" ID="SourceFd" SelectionMode="Multiple" Width="140" Rows="8" /></td>
<td><AllButton:Butn runat="server" id="ChoiceField" SourceList="SourceFd" TargetList="TargetFd" /></td>
<td><asp:ListBox Runat="server" ID="TargetFd" Width="140" Rows="8" /></td></tr>
<tr><td align="center" colspan="3"><asp:button id="b1" OnClick="GetRecord" Text="确定" Runat="server" /><br><asp:label id="nn" runat="server" /></td></tr>
</table>
<hr>
      <ASP:DataGrid id="Cus" runat="server"
        BorderColor="black"
        BorderWidth="1"
        GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="True"
>
</ASP:DataGrid>

</form>
</body>