2

I'm working with asp.net and would like to dynamically generate multiple tables, and then display each table on the webpage. So

In the Environments.aspx.cs file:

  1. Query the database to obtain the data and discover how many tables should be created
  2. Create and define each table (whether 1 or 50) using a loop. Each table has 6 columns and the rows depend on the data returned

Possibly in the Environments.aspx file:

  1. Display each table that was created. I should have full control of the table placement.

I would like to avoid creating one large table to maintain a user friendly look. If someone could provide a short code example, I'd appreciate it.

Win
  • 61,100
  • 13
  • 102
  • 181
Wario X
  • 203
  • 3
  • 10
  • Are you querying data from a single table or multiple tables? On what basis are you dividing data in multiple tables? How are you planning your code to DISCOVER how many tables should be created? – ZedBee Feb 20 '13 at 20:57
  • Multiple tables where the data is divided based on a certain column. I think I can best answer your questions by giving you the queries: **All necessary data:** _SELECT ch.Servername, ch.Environment, ch.Users, ser.VersionNumber, ch.Notes, ser.Linkaddress FROM Automation.dbo.[CheckoutMap] ch JOIN Automation.dbo.[Servers] ser on ch.Servername = ser.Servername ORDER BY ch.DisplayOrder_ **Amount of tables:** _select count(distinct ch.Environment) FROM Automation.dbo.[CheckoutMap] ch JOIN Automation.dbo.[Servers] ser on ch.Servername = ser.Servername_ – Wario X Feb 20 '13 at 21:21

2 Answers2

3

Here is a sample. I hope it will get you keep going.

enter image description here

<asp:ListView runat="server" ID="ListView1" ItemPlaceholderID="Panel1" 
    OnItemDataBound="ListView_ItemDataBound">
    <LayoutTemplate>
        <asp:Panel runat="server" ID="Panel1"></asp:Panel>
    </LayoutTemplate>
    <ItemTemplate>
        <%#Eval("Name") %>
        <asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="LastName" HeaderText="LastName" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
            </Columns>
        </asp:GridView>
    </ItemTemplate>
</asp:ListView>

public class House
{
    public int HouseId { get; set; }
    public string Name { get; set; }
    public List<User> Users { get; set; }
}

public class User
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public List<House> Houses
{
    get
    {
        return new List<House>
            {
                new House
                    {
                        HouseId = 1,
                        Name = "House One",
                        Users = new List<User>()
                            {
                                new User {UserId = 1, FirstName = "John", LastName = "Newton"},
                                new User {UserId = 2, FirstName = "Marry", LastName = "Newton"},
                                new User {UserId = 3, FirstName = "Joe", LastName = "Newton"}
                            }
                    },
                new House
                    {
                        HouseId = 1,
                        Name = "House Two",
                        Users = new List<User>()
                            {
                                new User {UserId = 6, FirstName = "Newton", LastName = "Doe"},
                                new User {UserId = 7, FirstName = "Jack", LastName = "Doe"},
                                new User {UserId = 8, FirstName = "Lewis", LastName = "Doe"}
                            }
                    }
            };
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ListView1.DataSource = Houses;
        ListView1.DataBind();
    }
}

protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    var house = e.Item.DataItem as House;
    var gridView = e.Item.FindControl("GridView1") as GridView;

    gridView.DataSource = house.Users;
    gridView.DataBind();
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • For now I'm just trying to get this exact sample code to work, but I'm told that "The name 'ListView1' does not exist in the current context". Originating from the Page_Load method. – Wario X Feb 20 '13 at 22:06
  • Did you put the asp:ListView with named ListView1 in aspx page? – Win Feb 20 '13 at 22:20
  • Yep... ID of ListView1. Everything looks like it should work, so it may just be something local to my machine. – Wario X Feb 20 '13 at 22:46
  • The way my project was setup, I had to add two lines of code in the Page_Load method: 1. _ListView ListView1;_ 2. _ListView1 = (ListView)LoginView2.FindControl("ListView1");_. '_LoginView2_' was specific to my project. Anyway, thanks a lot for the help! – Wario X Feb 21 '13 at 16:13
1

Not sure if I got what you asked right. I see in your comment you got 1 select command. Else, can you give an example of what it should look like exactly?

If you want a way to get something where all the tables look about the same, you can try with a repeater.

Edit : Using a gridview might be easier then a table since you only need to bind in the ItemDataBound the dataset to the gridview in the repeater.

Environments.aspx :

<asp:Repeater ID="repeatExample" runat="server" OnItemDataBound="repeatExample_ItemDataBound">
     <ItemTemplate>
          //Place table here
     </ItemTemplate>
</asp:Repeater>

Environments.aspx.cs :

private void bindRepeater()
    {
        //Get all your info using a stored procedure
        //ExampleClassDAL being where my code to call the stored proc is and return a 
        //List of DataSets (You would have to code a way to get multiple dataset for each table in your page.)
        List<DataSet> dsExample = ExampleClassDAL.StoredProcGetAllTheStuff();
        //Add extra code here if needed
        [...]
        //Bind the repeater
        repeatTournois.DataSource = dsExample ;
        repeatTournois.DataBind();
    }

 protected void repeatExample_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
       //Fill your table here using the DataSource
       DataSet ds = ((DataSet)e.Item.DataItem);
       [...]
    }

Hope this help.

P.S. I didn't try the List of DataSet. It might not work.

snaplemouton
  • 1,459
  • 14
  • 28
  • The answer from Win has a picture of exactly what I'm looking for. – Wario X Feb 20 '13 at 22:15
  • Then you can use what he did. A gridview inside a list view work. A repeater give you more freedom then a list view though. (At least I never managed to get much out of a list view.) – snaplemouton Feb 20 '13 at 22:20