Solved

adding items to a datalist?

Posted on 2004-09-30
13
630 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 500 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

621 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