Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 854
  • Last Modified:

how do i display multiple gridviews on a single asp.net page?

Hi,

I want to dynamically create multiple gridviews on a single asp.net 3.5 page.  Its for a machine booking site, and i need to dynamically pass to each gridivew the id of the machine (from the database) and today's date.

Can this be done?

At the moment i have only one gridview rendered.
0
Juliafrazer
Asked:
Juliafrazer
  • 3
  • 2
  • 2
1 Solution
 
CtrlAltDlCommented:
That is not a problem, just give each gridview a unique ID.
0
 
JuliafrazerAuthor Commented:
but i dont know how many gridviews i will need. It depends on the number of machines this booking system will have.  The idea is the administrator will add a new machine whenever they want, so the main page needs to dynamically create a seperate gridview for each machine detected on the database....

...any clues?

Julia
0
 
CtrlAltDlCommented:
Ok, now I understand what your getting too, sorry.

What you want to do is create a gridview programatically:

Like this:
    // Create a new GridView control.
    GridView customersGridView = new GridView();
 
    // Set the GridView control's properties.
    customersGridView.ID = "CustomersGridView";
    customersGridView.DataSourceID = "CustomersSqlDataSource";
    customersGridView.AutoGenerateColumns = true;
    customersGridView.AllowPaging = true;

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jmwheelerCommented:
To expand on the example above:

Make sure you do this when the page loads and for every post back.  If you don't create the controls on post back they will disappear.
protected void Page_Load(object Sender, EventArgs e)
{
   GridView customersGridView = new GridView();
   customersGridView.ID = "CustomersGridView";
   customersGridView.DataSourceID = "CustomersSqlDataSource";
   customersGridView.AutoGenerateColumns = true;
   customersGridView.AllowPaging = true;
   Page.Controls.Add(customersGridView);
}

Open in new window

0
 
JuliafrazerAuthor Commented:
thanks, but im not sure this is working....when i try to run it, it comes with this error message...

Control 'GridViewTest' of type 'GridView' must be placed inside a form tag with runat=server.

I havent created the gridview on the html page, as in theory i dont know how many i will require...it needs to be generated dynamically from a machineID count in the database.

Any clues?

Thanks

Julia
0
 
jmwheelerCommented:
Make sure your page or master page has a form tag

<form id="form1" runat="server">

Then change

Page.Controls.Add(customerGridView);

to

Form.Controls.Add(customerGridView);
0
 
JuliafrazerAuthor Commented:
I need to convert the following gridview format to the server side code...how would i go about doing this, then i can generate multiple gridviews automatically...
<asp:GridView ID="BookingsList1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1">
        <Columns>
        
        <asp:TemplateField>
        <ItemTemplate>
     
     <td>
        <asp:Label ID="lblSlotID" runat="server" visible="false" Text='<%#DataBinder.Eval(Container.DataItem, "SlotID")%>'></asp:Label>
        
        </td>
        <td>
     
        <td>
        <asp:Label ID="lblSlotTime" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "SlotTime")%>'></asp:Label>
        
        </td>
        <td>
        
        <asp:Label ID="lblTableDate1" runat="server" visible='<%#ShowLabel(DataBinder.Eval(Container.DataItem,"TableDate1").ToString())%>'  Text='<%#DataBinder.Eval(Container.DataItem, "TableDate1")%>'></asp:Label>
      
      <asp:CheckBox ID="chkBookingDate1" runat="server" visible='<%#ShowBookings(DataBinder.Eval(Container.DataItem,"TableDate1").ToString())%>'></asp:CheckBox>
      <asp:label ID="lblBoxTableDate1" runat="server" visible='<%#ShowBookings(DataBinder.Eval(Container.DataItem,"TableDate1").ToString())%>' Text='<%#DataBinder.Eval(Container.DataItem, "TableDate1")%>'></asp:Label>
      
        </td>
        
          <td>
        
        <asp:Label ID="lblTableDate2" runat="server" visible='<%#ShowLabel(DataBinder.Eval(Container.DataItem,"TableDate2").ToString())%>'  Text='<%#DataBinder.Eval(Container.DataItem, "TableDate2")%>'></asp:Label>
      
      <asp:CheckBox ID="chkBookingDate2" runat="server" visible='<%#ShowBookings(DataBinder.Eval(Container.DataItem,"TableDate2").ToString())%>'></asp:CheckBox>
      <asp:label ID="lblBoxTableDate2" runat="server" visible='<%#ShowBookings(DataBinder.Eval(Container.DataItem,"TableDate2").ToString())%>' Text='<%#DataBinder.Eval(Container.DataItem, "TableDate2")%>'></asp:Label>
      
        </td>
          <td>
        
        <asp:Label ID="lblTableDate3" runat="server" visible='<%#ShowLabel(DataBinder.Eval(Container.DataItem,"TableDate3").ToString())%>'  Text='<%#DataBinder.Eval(Container.DataItem, "TableDate3")%>'></asp:Label>
      
      <asp:CheckBox ID="chkBookingDate3" runat="server" visible='<%#ShowBookings(DataBinder.Eval(Container.DataItem,"TableDate3").ToString())%>'></asp:CheckBox>
      <asp:label ID="lblBoxTableDate3" runat="server" visible='<%#ShowBookings(DataBinder.Eval(Container.DataItem,"TableDate3").ToString())%>' Text='<%#DataBinder.Eval(Container.DataItem, "TableDate3")%>'></asp:Label>
      
        </td>
        
           <td>
        
        <asp:Label ID="lblTableDate4" runat="server" visible='<%#ShowLabel(DataBinder.Eval(Container.DataItem,"TableDate4").ToString())%>'  Text='<%#DataBinder.Eval(Container.DataItem, "TableDate4")%>'></asp:Label>
      
      <asp:CheckBox ID="chkBookingDate4" runat="server" visible='<%#ShowBookings(DataBinder.Eval(Container.DataItem,"TableDate4").ToString())%>'></asp:CheckBox>
      <asp:label ID="lblBoxTableDate4" runat="server" visible='<%#ShowBookings(DataBinder.Eval(Container.DataItem,"TableDate4").ToString())%>' Text='<%#DataBinder.Eval(Container.DataItem, "TableDate4")%>'></asp:Label>
      
        </td>
        
            <td>
        
        <asp:Label ID="lblTableDate5" runat="server" visible='<%#ShowLabel(DataBinder.Eval(Container.DataItem,"TableDate5").ToString())%>'  Text='<%#DataBinder.Eval(Container.DataItem, "TableDate5")%>'></asp:Label>
      
      <asp:CheckBox ID="chkBookingDate5" runat="server" visible='<%#ShowBookings(DataBinder.Eval(Container.DataItem,"TableDate5").ToString())%>'></asp:CheckBox>
      <asp:label ID="lblBoxTableDate5" runat="server" visible='<%#ShowBookings(DataBinder.Eval(Container.DataItem,"TableDate5").ToString())%>' Text='<%#DataBinder.Eval(Container.DataItem, "TableDate5")%>'></asp:Label>
      
        </td>
        
           <td>
        
        <asp:Label ID="lblTableDate6" runat="server" visible='<%#ShowLabel(DataBinder.Eval(Container.DataItem,"TableDate6").ToString())%>'  Text='<%#DataBinder.Eval(Container.DataItem, "TableDate6")%>'></asp:Label>
      
      <asp:CheckBox ID="chkBookingDate6" runat="server" visible='<%#ShowBookings(DataBinder.Eval(Container.DataItem,"TableDate6").ToString())%>'></asp:CheckBox>
      <asp:label ID="lblBoxTableDate6" runat="server" visible='<%#ShowBookings(DataBinder.Eval(Container.DataItem,"TableDate6").ToString())%>' Text='<%#DataBinder.Eval(Container.DataItem, "TableDate6")%>'></asp:Label>
      
        </td>
        
        
           </ItemTemplate>
        </asp:TemplateField>
         
        </Columns>
    </asp:GridView>

Open in new window

0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now