任何人都可以請幫我我在ASP.NET中有一個表叫:info,有3列:id,name,code
我想在一個名爲Default.aspx的頁面上有一個checkboxlist顯示錶的列,並且當用戶選擇他想要查看的列時,然後他按下一個按鈕將其重定向到另一個頁面並以Gridview顯示(gridview結果顯示在不同的頁面中,這非常重要)在Gridview中的不同頁面顯示選定的列
第1頁:
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server" BackColor="#FFFFCC" Height="165px" Width="155px">
<asp:CheckBoxList ID="CheckBoxList1" runat="server"></asp:CheckBoxList>
<br />
</asp:Panel>
</div>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 26px" Text="Button" />
<br />
</form>
落後第1頁代碼:
SqlConnection con = new SqlConnection("Server=.\\SQLEXPRESS;Database = ProiectWeb; Integrated Security=True");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
loadData();
}
}
private void loadData()
{
cmd.Connection = con;
cmd.CommandText = "SELECT column_name FROM information_schema.columns WHERE table_name ='info' ORDER BY ordinal_position";
con.Open();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
CheckBoxList1.Items.Add(reader[0].ToString());
}
}
reader.Close();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
}
第2頁:
<asp:Panel ID="Panel2" runat="server" Height="302px" Width="278px">
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" style="margin-left: 0px">
</asp:GridView>
</asp:Panel>
代碼頁背後2:
SqlConnection con = new SqlConnection("Server=.\\SQLEXPRESS;Database = ProiectWeb; Integrated Security=True");
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
cmd.Connection = con;
loadGrid("SELECT * FROM info");
}
}
public void loadGrid(string query)
{
DataSet data = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(query,con);
adapter.Fill(data);
GridView1.DataSource = data;
GridView1.DataBind();
}
你到目前爲止嘗試過什麼嗎?如果是這樣,發佈代碼,你面臨的問題! – 2014-10-26 14:02:02
我推測了我直到現在仍在使用的代碼 – tenebrez123 2014-10-26 17:20:01
首先,您必須將選定的列傳遞到第二頁,然後在第二頁中使用動態SQL來選擇數據並綁定到gridview或簡單地打開所需的列gridview並使其隱藏。 – 2014-10-26 17:29:02