Solved

adding items to a datalist?

Posted on 2004-09-30
13
612 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
  • 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now