0

Can anyone please help me i have in ASP.NET A Table called : info , with 3 columns : id,name,code I want on a page called Default.aspx to have a checkboxlist that shows the table's Columns and when the user selects what columns he wants to see , then he presses a button to redirect him to Another Page and be shown in a Gridview (the gridview results to be show in a different page it's very important)

Page 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 />
         &nbsp;
    </asp:Panel>      
</div>
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 26px" Text="Button" />
    <br />
</form>

Code behind Page 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)
{


}  

Page 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>

Code Behind Page 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();
}

1 Answers1

1

You need to follow, this.

  1. In Button1_click event, get selected list of selected checkboxes and post them as query string to second page. I know querystring has length limitation and there are other way of passing values ( post data, session ..), but still if column list is less, you are good to go with this approach.

Following code will get list of selected columns, and redirect to second page.

protected void Button1_Click(object sender, EventArgs e)
{
    List<string> selected = CheckBoxList1.Items.Cast<ListItem>()
        .Where(li => li.Selected)
        .Select(li => li.Text)
        .ToList();
    string sel = string.Join(",", selected);
    Response.Redirect("Second.aspx?cols=" + sel);
}
  1. In second page , page_load event read the querystring cols , and use Row_databound event of gridview to make rest of columns hidden except those passed in as querystring.

For this case, you can refer - GridView Hide Column by code OR Hide a GridView column by name at runtime in ASP.Net

Again, there are options to implement this, like generate dynamic SQL to get only required data and bind that to gridview. But the above seems simple one, that is why i explained in that way.

Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48