Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

adding items to a datalist?

Posted on 2004-09-30
13
Medium Priority
?
631 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:dotnet22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12190960
[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
0
 

Author Comment

by:dotnet22
ID: 12191023
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?
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12191067
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 17

Expert Comment

by:AerosSaga
ID: 12191117
The ide handles the alternating & item style
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12191120
You just set it for the particular datalist
0
 

Author Comment

by:dotnet22
ID: 12191129
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?
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12191204
are you sure your dataset is not empty?
0
 

Author Comment

by:dotnet22
ID: 12191297
yes. I'm checking it and it is definetly not empty...
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 2000 total points
ID: 12191998
maybe because I'm creating a dataview and creating that to bind with as you are just using the default view of a dataset:
 Dim dv As New DataView(dt)

vs

DataList1.DataSource = ds.Tables[0].DefaultView;

  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

 DataList1.DataSource = CreateDataSource()
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12192006
[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
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12192761
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
0
 

Author Comment

by:dotnet22
ID: 12193326
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?
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12198215
may be it is easier  if you post the entire code

B..M
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question