Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
| <%@ Page Language="C#" MasterPageFile="~/maquette.master" Trace="true" AutoEventWireup="true" CodeFile="MaintainScrollPositionOnPostbackDemo.aspx.cs" Inherits="MaintainScrollPositionOnPostbackDemo" Title="Untitled Page" MaintainScrollPositionOnPostback="true" %> <asp:Content ID="Content1" ContentPlaceHolderID="cphContenu" Runat="Server"> </asp:Content> |
| <%@ Page Language="C#" MasterPageFile="~/maquette.master" Trace="true" AutoEventWireup="true" CodeFile="MaintainScrollPositionOnPostbackDemo.aspx.cs" Inherits="MaintainScrollPositionOnPostbackDemo" Title="Untitled Page" MaintainScrollPositionOnPostback="true" %> <asp:Content ID="Content1" ContentPlaceHolderID="cphContenu" Runat="Server"> </asp:Content> |
| <system.web> <trace enabled="true" traceMode="SortByCategory" localOnly="true"/> |
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections; namespace ControlLibrary { [ToolboxData("<{0}:SimpleDataBoundColumn runat=server></{0}:SimpleDataBoundColumn>")] public class SimpleDataBoundColumn : DataBoundControl { public string DataTextField { get { object o = ViewState["DataTextField"]; return ((o == null) ? string.Empty : (string)o); } set { ViewState["DataTextField"] = value; if (Initialized) { OnDataPropertyChanged(); } } } protected override void PerformSelect() { // si DataSourceID du control n'est pas définie if (!IsBoundUsingDataSourceID) this.OnDataBinding(EventArgs.Empty); GetData().Select(CreateDataSourceSelectArguments(),this.OnDataSourceViewSelectCallback); RequiresDataBinding = false; MarkAsDataBound(); // declenchement evenement databound OnDataBound(EventArgs.Empty); } private void OnDataSourceViewSelectCallback(IEnumerable retrievedData) { // DataSourceID du control est définie if (IsBoundUsingDataSourceID) OnDataBinding(EventArgs.Empty); PerformDataBinding(retrievedData); } protected override void PerformDataBinding(IEnumerable retrievedData) { base.PerformDataBinding(retrievedData); int index = 0; if (retrievedData != null) { Table table = new Table(); table.BorderWidth = 1; foreach (object dataItem in retrievedData) { TableRow row = new TableRow(); if (DataTextField.Length > 0) { TableCell cell = new TableCell(); cell.Text = DataBinder.GetPropertyValue(dataItem, DataTextField, null); row.Cells.Add(cell); } else { PropertyDescriptorCollection propertyDescriptors = TypeDescriptor.GetProperties(dataItem); // création du header if (index == 0) { TableRow headerRow = new TableRow(); headerRow.BackColor = System.Drawing.Color.LightGray; foreach (PropertyDescriptor propertyDescriptor in propertyDescriptors) { TableCell cell = new TableCell(); cell.Text = propertyDescriptor.Name; headerRow.Cells.Add(cell); } table.Rows.Add(headerRow); index += 1; } // création des lignes de données if (propertyDescriptors.Count >= 1) { foreach (PropertyDescriptor propertyDescriptor in propertyDescriptors) { if (propertyDescriptor.GetValue(dataItem) != null) { TableCell cell = new TableCell(); cell.Text = propertyDescriptor.GetValue(dataItem).ToString(); row.Cells.Add(cell); } } } } table.Rows.Add(row); } this.Controls.Add(table); } } } } |
| <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="ControlLibrary" Namespace="ControlLibrary" TagPrefix="cc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Page sans titre</title> </head> <body> <form id="form1" runat="server"> <div> <cc1:SimpleDataBoundColumn ID="SimpleDataBoundColumn1" runat="server" DataSourceID="SqlDataSource1" DataTextField="" /> <br /> <br /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ContactDBConnectionString %>" SelectCommand="SELECT [ContactID], [ContactName], [ContactFirstName], [ContactAge], [ContactCategoryID] FROM [Contact]"> </asp:SqlDataSource> <br /> <br /> </div> </form> |