Link to home
Start Free TrialLog in
Avatar of dotnet22
dotnet22

asked on

adding items to a datalist?

How can I add items programmatically to the datalist fields. For instance there is the ItemTemplate Field and AlternatingItemTemplate fields I guess.  How can I populate these fields?
Avatar of AerosSaga
AerosSaga

[Visual Basic]
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<html>
   <script runat="server">
 
      Function CreateDataSource() As ICollection

         Dim dt As New DataTable()
         Dim dr As DataRow
       
         dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
       
         Dim i As Integer

         For i = 0 To 9
            dr = dt.NewRow()
            dr(0) = "Item " & i.ToString()
            dt.Rows.Add(dr)
         Next i
         
         Dim dv As New DataView(dt)
         Return dv

      End Function 'CreateDataSource

      Sub Page_Load(sender As Object, e As EventArgs)

         If Not IsPostBack Then
            DataList1.DataSource = CreateDataSource()
            DataList1.DataBind()
         End If

      End Sub 'Page_Load


      Sub Button1_Click(sender As Object, e As EventArgs)
       
         If DropDown1.SelectedIndex = 0 Then
            DataList1.RepeatDirection = RepeatDirection.Horizontal
         Else
            DataList1.RepeatDirection = RepeatDirection.Vertical
         End If

         If DropDown2.SelectedIndex = 0 Then
            DataList1.RepeatLayout = RepeatLayout.Table
         Else
            DataList1.RepeatLayout = RepeatLayout.Flow
         End If

         DataList1.RepeatColumns = DropDown3.SelectedIndex + 1
       
         If Check1.Checked = True And DataList1.RepeatLayout = RepeatLayout.Table Then
            DataList1.BorderWidth = Unit.Pixel(1)
            DataList1.GridLines = GridLines.Both
         Else
            DataList1.BorderWidth = Unit.Pixel(0)
            DataList1.GridLines = GridLines.None
         End If

      End Sub 'Button1_Click
 
   </script>
 
<body>
 
   <form runat="server">

      <h3>DataList Example</h3>
 
      <asp:DataList id="DataList1" runat="server"
           BorderColor="black"
           CellPadding="3"
           Font-Name="Verdana"
           Font-Size="8pt">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

         <HeaderTemplate>
            Items
         </HeaderTemplate>
               
         <ItemTemplate>
            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>
         </ItemTemplate>
 
      </asp:DataList>
      <p>
      <hr noshade align="left" width="300px">
      RepeatDirection:
      <asp:DropDownList id=DropDown1 runat="server">
         <asp:ListItem>Horizontal</asp:ListItem>
         <asp:ListItem>Vertical</asp:ListItem>
      </asp:DropDownList><br>
      RepeatLayout:
      <asp:DropDownList id=DropDown2 runat="server">
         <asp:ListItem>Table</asp:ListItem>
         <asp:ListItem>Flow</asp:ListItem>
      </asp:DropDownList><br>
      RepeatColumns:
      <asp:DropDownList id=DropDown3 runat="server">
         <asp:ListItem>1</asp:ListItem>
         <asp:ListItem>2</asp:ListItem>
         <asp:ListItem>3</asp:ListItem>
         <asp:ListItem>4</asp:ListItem>
         <asp:ListItem>5</asp:ListItem>
      </asp:DropDownList><br>
      Show Borders:
      <asp:CheckBox id=Check1 runat="server" /><p>
      <asp:LinkButton id=Button1
           Text="Refresh DataList"
           OnClick="Button1_Click"
           runat="server"/>
   </form>
 
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<html>
   <script runat="server">

      ICollection CreateDataSource()
      {
         DataTable dt = new DataTable();
         DataRow dr;

         dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
         for (int i = 0; i < 10; i++)
         {
            dr = dt.NewRow();
            dr[0] = "Item " + i.ToString();
            dt.Rows.Add(dr);
         }

         DataView dv = new DataView(dt);
         return dv;
      }

      void Page_Load(Object Sender, EventArgs e)
      {
         if (!IsPostBack)
         {
            DataList1.DataSource = CreateDataSource();
            DataList1.DataBind();
         }
      }

      void Button1_Click(Object Sender, EventArgs e)
      {

         if (DropDown1.SelectedIndex == 0)
            DataList1.RepeatDirection = RepeatDirection.Horizontal;
         else
            DataList1.RepeatDirection = RepeatDirection.Vertical;
         if (DropDown2.SelectedIndex == 0)
            DataList1.RepeatLayout = RepeatLayout.Table;
         else
            DataList1.RepeatLayout = RepeatLayout.Flow;
         DataList1.RepeatColumns=DropDown3.SelectedIndex+1;
         if ((Check1.Checked ==true) && 
            (DataList1.RepeatLayout == RepeatLayout.Table))
         {
            DataList1.BorderWidth = Unit.Pixel(1);
            DataList1.GridLines = GridLines.Both;
         }    
         else  
         {
            DataList1.BorderWidth = Unit.Pixel(0);
            DataList1.GridLines = GridLines.None;
         }    
      }    

   </script>

<body>

   <form runat="server">
      <h3>DataList Sample</h3>

         <asp:DataList id="DataList1"
              BorderColor="black"
              CellPadding="3"
              Font-Name="Verdana"
              Font-Size="8pt"
              runat="server">

            <HeaderStyle BackColor="#aaaadd"/>
            <AlternatingItemStyle BackColor="Gainsboro"/>

            <HeaderTemplate>
               Items
            </HeaderTemplate>
            <ItemTemplate>
               <%# DataBinder.Eval(Container.DataItem, "StringValue") %>
            </ItemTemplate>

         </asp:DataList>
         <p>
         <hr noshade align="left" width="300px">
         RepeatDirection:
         <asp:DropDownList id=DropDown1 runat="server">
            <asp:ListItem>Horizontal</asp:ListItem>
            <asp:ListItem>Vertical</asp:ListItem>
         </asp:DropDownList><br>
         RepeatLayout:
         <asp:DropDownList id=DropDown2 runat="server">
            <asp:ListItem>Table</asp:ListItem>
            <asp:ListItem>Flow</asp:ListItem>
         </asp:DropDownList><br>
         RepeatColumns:
         <asp:DropDownList id=DropDown3 runat="server">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
            <asp:ListItem>4</asp:ListItem>
            <asp:ListItem>5</asp:ListItem>
         </asp:DropDownList><br>
         Show Borders:
         <asp:CheckBox id=Check1 runat="server" />
         <p>
         <asp:LinkButton id=Button1
              Text="Refresh DataList"
              OnClick="Button1_Click"
              runat="server"/>
      </font>
   </form>

</body>
</html>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpcondatalistwebservercontrol.asp

Regards,

Aeros
Avatar of dotnet22

ASKER

My items are coming from a database:

string connString = ConfigurationSettings.AppSettings["NorthWind"];
                  ds = new DataSet();
                  SqlConnection conn = new SqlConnection(connString);
                  string select = "SELECT * FROM Products";
                  sqlAdapter = new SqlDataAdapter(select,conn);
                  sqlAdapter.Fill(ds);
                  DataList1.DataSource = ds.Tables[0].DefaultView;
                  
                  DataList1.DataBind();

this doesn't do anything? I'm trying to populate the individual field like ItemTemplate and AlternatingTemplate?
Ok then check here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsbasedatalistclassdatasourcetopic.asp
Function CreateDataSource() As ICollection
        Dim dt As New DataTable()
        Dim dr As DataRow
       
        dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
        dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
        dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
       
        Dim i As Integer
        For i = 0 To 8
            dr = dt.NewRow()
           
            dr(0) = i
            dr(1) = "Item " + i.ToString()
            dr(2) = 1.23 *(i + 1)
           
            dt.Rows.Add(dr)
        Next i
       
        Dim dv As New DataView(dt)
        Return dv
  End Function 'CreateDataSource

The ide handles the alternating & item style
You just set it for the particular datalist
I'm still confused on why my source code isnt' working? All the data is in the dataset... Why do I need to create a DataTable and datarows?
are you sure your dataset is not empty?
yes. I'm checking it and it is definetly not empty...
ASKER CERTIFIED SOLUTION
Avatar of AerosSaga
AerosSaga

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
[Visual Basic]
Private Sub MakeDataView(ds As DataSet)
   Dim dv As New DataView(ds.Tables("Suppliers"), _
                          "Country = 'UK'", _
                          "CompanyName", _
                          DataViewRowState.CurrentRows)
   dv.AllowEdit = True
   dv.AllowNew = True
   dv.AllowDelete = True
End Sub 'MakeDataView
End Class 'Sample

[C#]
private void MakeDataView(DataSet ds)
{
   DataView dv = new DataView(ds.Tables["Suppliers"], "Country = 'UK'", "CompanyName", DataViewRowState.CurrentRows);
   dv.AllowEdit = true;
   dv.AllowNew = true;
   dv.AllowDelete = true;
}

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadataviewclassctortopic.asp
Hi dotnet22,

have you tried like this:

               ds = new DataSet();
               SqlConnection conn = new SqlConnection(connString);
               string select = "SELECT * FROM Products";
               sqlAdapter = new SqlDataAdapter(select,conn);
               sqlAdapter.Fill(ds);
               DataList1.DataSource = ds.Tables[0];
               
               DataList1.DataBind();

B..M
Yes. I've tried that. I think the problem might be the fact that I'm using the visual studio designer without touching the html? do we need to do anything with the html?
may be it is easier  if you post the entire code

B..M