DataList in GridView

Source: Internet
Author: User
Tags id3

The GridView nested DataList describes how to load data display using three different carriers in DataList.

Directory

  • Using PlaceHolder load UserControl
  • Directly Add data to DataList.
  • Use UserControl to nest DatList

1. Using PlaceHolder load UserControl

WebForm:

<Asp: GridView ID = "GridView1" runat = "server" OnRowDataBound = "GridView1_RowDataBound">
<Columns>
<Asp: TemplateField>
<ItemTemplate>
<% # Eval ("Orderid") %>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField>
<ItemTemplate>
<Asp: PlaceHolder ID = "place" runat = "server"> </asp: PlaceHolder>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>

UserControl:

<Asp: DataList ID = "DataList2" runat = "server" RepeatDirection = "Horizontal" GridLines = "Both">
<ItemTemplate>
Orderid: <% # Eval ("Orderid") %>
Unitprice: <% # Eval ("unitprice") %>
</ItemTemplate>
</Asp: DataList>

WebForm Code Bebind:

First, bind the GridView to the Page_Load event.

Then: In the GridView_RowDataBound event

IList <String> str = new List <String> (); // Add Test Data
Str. Add ("ID1 ");
Str. Add ("ID2 ");
Str. Add ("ID3 ");
Str. Add ("ID3 ");

If (e. Row. RowIndex>-1)
{
For (int I = 0; I <str. Count; I ++)
{
If (e. Row. RowIndex = I) // remove the loaded duplicate items.
{
// Method 1
PlaceHolder pl = e. Row. FindControl ("place") as PlaceHolder;
WebUserControl1 c = this. Page. LoadControl ("WebUserControl1.ascx") as WebUserControl1;

SqlDataAdapter sqlDataAdapter;
DataSet dataSet = new DataSet ();

SqlDataAdapter = new SqlDataAdapter (SQL statement, sqlConnection );
SqlDataAdapter. SelectCommand. Parameters. Add ("@ orderid", SqlDbType. Int). Value = OrderId Value

SqlDataAdapter. Fill (dataSet, "[order details]");
C. DataSource = dataSet. Tables ["[order details]"];

// Method 1
Pl. Controls. Add (c );
}
}
}
}

UC CodeBebind:

Protected void Page_Load (object sender, EventArgs e)
{
This. DataList2.DataSource = dataSource;
This. DataBind ();
}

Private DataTable dataSource;
Public DataTable DataSource
{
Set
{
DataSource = value;
}
}

2. directly Add data to DataList, and display

WebForm:

<Asp: GridView ID = "GridView1" runat = "server" OnRowDataBound = "GridView1_RowDataBound">
<Columns>
<Asp: TemplateField>
<ItemTemplate>
<% # Eval ("Orderid") %>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField>
<ItemTemplate>
<Asp: DataList ID = "DataList2" runat = "server" RepeatDirection = "Horizontal">
<ItemTemplate>
Orderid: <% # Eval ("Orderid") %>
Unitprice: <% # Eval ("unitprice") %>
</ItemTemplate>
<Asp: DataList>

</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>

WebForm Code Bebind:

First, bind the GridView to the Page_Load event.

Then: In the GridView_RowDataBound event

IList <String> str = new List <String> (); // Add Test Data
Str. Add ("ID1 ");
Str. Add ("ID2 ");
Str. Add ("ID3 ");
Str. Add ("ID3 ");

If (e. Row. RowIndex>-1)
{
For (int I = 0; I <str. Count; I ++)
{
If (e. Row. RowIndex = I) // remove the loaded duplicate items.
{
// Method 2
DataList dl = e. Row. FindControl ("DataList2") as DataList;

SqlDataAdapter sqlDataAdapter;
DataSet dataSet = new DataSet ();

SqlDataAdapter = new SqlDataAdapter (SQL statement, sqlConnection );
SqlDataAdapter. SelectCommand. Parameters. Add ("@ orderid", SqlDbType. Int). Value = OrderId Value

SqlDataAdapter. Fill (dataSet, "[order details]");
C. DataSource = dataSet. Tables ["[order details]"];

Dl. DataSource = dataSet. Tables ["[order details]"];
Dl. DataBind ();

}
}
}
}

3. Use UserControl to nest DatList

WebForm:

<% @ Register Src = "WebUserControl1.ascx" TagName = "WebUserControl1" TagPrefix = "uc1" %>

<Asp: GridView ID = "GridView1" runat = "server" OnRowDataBound = "GridView1_RowDataBound">
<Columns>
<Asp: TemplateField>
<ItemTemplate>
<% # Eval ("Orderid") %>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField>
<ItemTemplate>

</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>

UserControl:

<Asp: DataList ID = "DataList2" runat = "server" RepeatDirection = "Horizontal" GridLines = "Both">
<ItemTemplate>
Orderid: <% # Eval ("Orderid") %>
Unitprice: <% # Eval ("unitprice") %>
</ItemTemplate>
</Asp: DataList>

WebForm Code Bebind:

First, bind the GridView to the Page_Load event.

Then: In the GridView_RowDataBound event

IList <String> str = new List <String> (); // Add Test Data
Str. Add ("ID1 ");
Str. Add ("ID2 ");
Str. Add ("ID3 ");
Str. Add ("ID3 ");

If (e. Row. RowIndex>-1)
{
For (int I = 0; I <str. Count; I ++)
{
If (e. Row. RowIndex = I) // remove the loaded duplicate items.
{
// Method 3
WebUserControl1 c = e. Row. FindControl ("WebUserControl1") as WebUserControl1;

SqlDataAdapter sqlDataAdapter;
DataSet dataSet = new DataSet ();

SqlDataAdapter = new SqlDataAdapter (SQL statement, sqlConnection );
SqlDataAdapter. SelectCommand. Parameters. Add ("@ orderid", SqlDbType. Int). Value = OrderId Value

SqlDataAdapter. Fill (dataSet, "[order details]");
C. DataSource = dataSet. Tables ["[order details]"];

}
}
}
}

UC CodeBebind:

Protected void Page_Load (object sender, EventArgs e)
{
This. DataList2.DataSource = dataSource;
This. DataBind ();
}

Private DataTable dataSource;
Public DataTable DataSource
{
Set
{
DataSource = value;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.