ASP.Net/Asp Control/DataList

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Bind arrayList to asp datalist (C#)

   <source lang="csharp">

<%@Page Language="C#"%> <html> <body> <asp:datalist id="dlTest" runat="server"

              forecolor="#000000"
              backcolor="#ffffff"
              cellpadding="3"
              gridlines="none"
              width="50%" >
 <itemstyle   font-name="tahoma,arial,sans-serif"
              font-size="12"
              backcolor="#ffffff" />
 <alternatingitemstyle font-name="tahoma,arial,sans-serif"
              font-size="2"
              backcolor="#ffff00" />
 <itemtemplate>
   <%# Container.DataItem %>
 </itemtemplate>

</asp:datalist> </body> </html> <script Language="C#" runat="server"> void Page_Load() {

  // create an ArrayList of values to bind to
  ArrayList arrValues = new ArrayList(4);
  arrValues.Add("A");
  arrValues.Add("B");
  arrValues.Add("C");
  arrValues.Add("D");
  arrValues.Add("E");
  // set the DataSource property of the control to the
  // array and bind it to display the values
  dlTest.DataSource = arrValues;
  dlTest.DataBind();

} </script>

      </source>
   
  


Bind hashtable to asp datalist (C#)

   <source lang="csharp">

<%@Page Language="C#"%> <html> <body> <asp:datalist id="dlTest" runat="server"

              forecolor="#000000"
              backcolor="#ffffff"
              cellpadding="3"
              gridlines="none"
              width="50%" >
 <itemstyle   font-name="tahoma,arial,sans-serif"
              backcolor="#ffffff" />
 <alternatingitemstyle font-name="tahoma,arial,sans-serif"
              backcolor="#ffff00" />
 <itemtemplate>
   <%# DataBinder.Eval(Container.DataItem, "Key", "{0}") %>
   <%# DataBinder.Eval(Container.DataItem, "Value", "${0:f2}") %>
 </itemtemplate>
 <alternatingitemtemplate>
   <%# DataBinder.Eval(Container.DataItem, "Key", "{0}") %>
   <%# DataBinder.Eval(Container.DataItem, "Value", "${0:f2}") %>
 </alternatingitemtemplate>

</asp:datalist> </body> </html> <script Language="C#" runat="server"> void Page_Load() {

  // create a HashTable of values to bind to
  Hashtable tabValues = new Hashtable(4);
  tabValues.Add("A", 49.56);
  tabValues.Add("B", 28.33);
  tabValues.Add("C", 55);
  tabValues.Add("D", 20.74);
  tabValues.Add("E", 41.1);
  // set the DataSource property of the control to the
  // hashtable and bind it to display the values
  dlTest.DataSource = tabValues;
  dlTest.DataBind();

} </script>

      </source>
   
  


item style and alternating item style for asp datalist (C#)

   <source lang="csharp">

<%@Page Language="C#"%> <html> <body> <asp:datalist id="dlTest" runat="server"

              forecolor="#000000"
              backcolor="#ffffff"
              cellpadding="3"
              gridlines="none"
              width="50%" >
 <itemstyle   font-name="tahoma,arial,sans-serif"
              font-size="12"
              backcolor="#ffffff" />
 <alternatingitemstyle font-name="tahoma,arial,sans-serif"
              font-size="2"
              backcolor="#ffff00" />
 <itemtemplate>
   <%# Container.DataItem %>
 </itemtemplate>

</asp:datalist> </body> </html> <script Language="C#" runat="server"> void Page_Load() {

  // create an ArrayList of values to bind to
  ArrayList arrValues = new ArrayList(4);
  arrValues.Add("A");
  arrValues.Add("B");
  arrValues.Add("C");
  arrValues.Add("D");
  arrValues.Add("E");
  // set the DataSource property of the control to the
  // array and bind it to display the values
  dlTest.DataSource = arrValues;
  dlTest.DataBind();

} </script>

      </source>