web:asp.net:toc
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
web:asp.net:toc [2014/02/12 02:26] – modification externe 127.0.0.1 | web:asp.net:toc [2023/10/04 01:32] (Version actuelle) – supprimée sgariepy | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== ASP.NET ====== | ||
- | * Voir aussi : [[ajax|ASP.NET AJAX]], [[mvc|ASP.NET MVC]] | ||
- | ===== Outils nécessaires ===== | ||
- | * Pour développer | ||
- | * Éditeur de texte | ||
- | * Framework .NET | ||
- | * Compilateur C# / VB.NET ou autre parmi ceux fournis avec .NET -> JScript, J# ou C++ .NET | ||
- | * Pour compiler -> '' | ||
- | * Pour tester / rouler | ||
- | * Serveur IIS | ||
- | ===== Outils souhaités ===== | ||
- | * **Visual Studio .NET** (2003 -> 2010), mais payant | ||
- | * **ASP.NET Development Server**, inclu avec Visual Studio | ||
- | * Webmatrix, mais probablement remplacé par Visual Web Developer 2008 Express | ||
- | * [[http:// | ||
- | |||
- | ====== Différences ====== | ||
- | |||
- | ^ Applications | ||
- | | WinForms | ||
- | | | ||
- | |||
- | |||
- | ====== Événements ====== | ||
- | |||
- | ===== Postback ===== | ||
- | Pour savoir si un Postback a été fait | ||
- | <code csharp> | ||
- | if (!Page.IsPostBack) | ||
- | { | ||
- | // | ||
- | } | ||
- | </ | ||
- | |||
- | ===== Référence ===== | ||
- | * {{: | ||
- | |||
- | ====== Code HTML dynamique ====== | ||
- | ===== Image avec query string ===== | ||
- | |||
- | Si on veut inclure une image, créée dynamiquement en ASP.NET (exemple '' | ||
- | |||
- | Dans le code '' | ||
- | <code asp> | ||
- | imgMonImage.ImageUrl = " | ||
- | </ | ||
- | Dans la page '' | ||
- | <code asp> | ||
- | < | ||
- | </ | ||
- | |||
- | Peut se faire aussi avec des autrement (style ASP)((À tester...)) : | ||
- | <code asp> | ||
- | < | ||
- | </ | ||
- | |||
- | ===== Récupérer les données en query string ===== | ||
- | |||
- | <code csharp> | ||
- | float x, y; | ||
- | if (!float.TryParse(context.Request.Params[" | ||
- | if (!float.TryParse(context.Request.Params[" | ||
- | </ | ||
- | |||
- | <code csharp> | ||
- | int id; | ||
- | if (int.TryParse(Request.QueryString[" | ||
- | // Faire le travail | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | ====== User controls ====== | ||
- | |||
- | Créer un //user control// est très simple, le fichier '' | ||
- | <code asp> | ||
- | <%@ Control Language=" | ||
- | CodeFile=" | ||
- | <table width=" | ||
- | <tr> | ||
- | < | ||
- | User Control Test Page</ | ||
- | </td> | ||
- | </tr> | ||
- | <tr> | ||
- | <td align=" | ||
- | Made by someone.</ | ||
- | </td> | ||
- | </tr> | ||
- | </ | ||
- | </ | ||
- | |||
- | Et dans la page ('' | ||
- | <code asp> | ||
- | <%@ Register TagPrefix=" | ||
- | </ | ||
- | |||
- | Et plus loin, à l' | ||
- | <code asp> | ||
- | <%@ Page Language=" | ||
- | Inherits=" | ||
- | <%@ Register TagPrefix=" | ||
- | < | ||
- | < | ||
- | < | ||
- | </ | ||
- | < | ||
- | <form id=" | ||
- | < | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | Créer des propriétés dans le code : | ||
- | <code csharp> | ||
- | public String Username | ||
- | { | ||
- | get {return _username;} | ||
- | set {_username = value;} | ||
- | } | ||
- | </ | ||
- | |||
- | Déclarer le contrôle dans '' | ||
- | <code xml> | ||
- | <add tagPrefix=" | ||
- | </ | ||
- | Cela permet d' | ||
- | |||
- | ====== Gestion d' | ||
- | ===== View State ===== | ||
- | La première approche pour garder les données, c'est la méthode // | ||
- | |||
- | <code csharp|Premier exemple pour une donnée simple> | ||
- | ViewState[" | ||
- | |||
- | // récupérer la valeur | ||
- | if (ViewState[" | ||
- | { | ||
- | compteur = (int)ViewState[" | ||
- | } | ||
- | </ | ||
- | |||
- | <code csharp|Autre exemple avec un objet (qui doit être serialisable)> | ||
- | [Serializable] | ||
- | public class Client | ||
- | { | ||
- | public string Prenom; | ||
- | public string Nom; | ||
- | public Customer(string prenom, string nom) | ||
- | { | ||
- | Prenom = this.prenom; | ||
- | Nom = this.nom; | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | <code csharp|Store un client dans un viewstate> | ||
- | Client clt = new Client(" | ||
- | ViewState[" | ||
- | </ | ||
- | |||
- | <code csharp|Propriété incorporant le ViewState> | ||
- | private int? _employeeId; | ||
- | private int? EmployeeId | ||
- | { | ||
- | get | ||
- | { | ||
- | if (_employeeId == null) | ||
- | { | ||
- | if (ViewState[" | ||
- | { | ||
- | _employeeId = (int)ViewState[" | ||
- | } | ||
- | else | ||
- | { | ||
- | _employeeId = null; | ||
- | } | ||
- | } | ||
- | return _employeeId; | ||
- | } | ||
- | set | ||
- | { | ||
- | _employeeId = value; | ||
- | if (value == null) | ||
- | { | ||
- | ViewState.Remove(" | ||
- | } | ||
- | else | ||
- | { | ||
- | ViewState[" | ||
- | } | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | |||
- | ===== Session State ===== | ||
- | S' | ||
- | |||
- | <code csharp> | ||
- | Session[" | ||
- | //ou | ||
- | Session.Add(" | ||
- | |||
- | // | ||
- | string nom = (string)Session(" | ||
- | </ | ||
- | |||
- | {{: | ||
- | |||
- | |||
- | ====== Afficher des messages d' | ||
- | <code csharp> | ||
- | Response.Buffer = true; | ||
- | Response.Clear(); | ||
- | Response.ContentType = " | ||
- | Response.Write(" | ||
- | </ | ||
- | |||
- | |||
- | Si une page personnalisée d' | ||
- | <code asp> | ||
- | public void Page_Error(object sender, EventArgs e) | ||
- | { | ||
- | Exception objErr = Server.GetLastError().GetBaseException(); | ||
- | string err = "< | ||
- | "< | ||
- | "< | ||
- | "< | ||
- | objErr.StackTrace.ToString(); | ||
- | Response.Write(err.ToString()); | ||
- | Server.ClearError(); | ||
- | } | ||
- | </ | ||
- | |||
- | ====== Localization ====== | ||
- | Pour aller chercher la langue envoyée par le navigateur web((Source : Pro ASP.NET 2.0 in C# 2005 (Apress, 2005), p.609. )) : | ||
- | <code csharp> | ||
- | using System.Globalization; | ||
- | |||
- | //... | ||
- | CultureInfo ci; | ||
- | if ((Request.UserLanguages != null) && (Request.UserLanguages.Length > 0)) | ||
- | { | ||
- | ci = new CultureInfo(Request.UserLanguages[0]); | ||
- | } | ||
- | else | ||
- | { | ||
- | ci = new CultureInfo(" | ||
- | } | ||
- | System.Threading.Thread.CurrentThread.CurrentUICulture = ci; | ||
- | </ | ||
- | |||
- | |||
- | ===== Par programmation ===== | ||
- | La localisation de chaînes de caractère peuvent se faire automatiquement avec l' | ||
- | |||
- | <code csharp> | ||
- | lblUnLabel.Text = Convert.ToString(GetLocalResourceObject(" | ||
- | </ | ||
- | |||
- | On peut aussi utiliser cette technique avec les textes dynamiques. | ||
- | <code csharp> | ||
- | this.Literal1.Text = string.Format(Convert.ToString(GetLocalResourceObject(" | ||
- | </ | ||
- | |||
- | ===== LCID ===== | ||
- | |||
- | ^ Culture | ||
- | | en-US | 1033 | | ||
- | | es-SP | 3082 | | ||
- | | fr-CA | 3084 | | ||
- | | ru | 1049 | | ||
- | |||
- | Autres : http:// | ||
- | |||
- | ===== Problèmes liés à la culture ===== | ||
- | Il est possible de rencontrer une erreur de style : | ||
- | Culture ' | ||
- | Cette erreur peut être visible que sur Firefox car il peut envoyer une culture sur deux lettres ('' | ||
- | |||
- | ===== Changer la culture par programmation ===== | ||
- | |||
- | <code csharp> | ||
- | System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(" | ||
- | </ | ||
- | ===== Utiliser un fichier ressource sans localisation ===== | ||
- | - Dans Visual Studio 2008, aller dans le menu **Projet**, **Propriétés de [Nom du projet]**. | ||
- | - Aller dans l' | ||
- | - Créer un nouveau fichier. | ||
- | - Le nouveau fichier '' | ||
- | - Entrer les données '' | ||
- | - Pour référencer une ressource en particulier :<code csharp> | ||
- | string s = SupportRequestApp.Properties.Resources.String1; | ||
- | </ | ||
- | - Il est aussi possible d' | ||
- | // Bonjour dans le resource file = " | ||
- | string s2 = string.Format(SupportRequestApp.Properties.Resources.Bonjour, | ||
- | </ | ||
- | Pas besoin de s' | ||
- | |||
- | ===== Ressources ===== | ||
- | http:// | ||
- | |||
- | ====== Envoyer un courriel ====== | ||
- | |||
- | <code csharp> | ||
- | using System.Net.Mail; | ||
- | using System.Net.Mime; | ||
- | |||
- | string strBody = " | ||
- | string strSubject = " | ||
- | string strFromAddr = " | ||
- | string strToAddr = " | ||
- | |||
- | MailMessage mmEmail = new MailMessage(strFromAddr, | ||
- | SmtpClient smtpclient = new SmtpClient(" | ||
- | NetworkCredential nc = new NetworkCredential(" | ||
- | smtpclient.Credentials = nc; | ||
- | |||
- | smtpclient.Send(mmEmail); | ||
- | </ | ||
- | |||
- | On peut mettre les informations relatives au serveur SMTP dans le '' | ||
- | <code xml> | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | En developpement, | ||
- | <code xml> | ||
- | < | ||
- | < | ||
- | < | ||
- | <smtp deliveryMethod=" | ||
- | < | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | |||
- | ====== Composants ASP.NET ====== | ||
- | ===== Événements ===== | ||
- | ^ Contrôle | ||
- | | GridView | ||
- | | GridView | ||
- | | GridView | ||
- | | Repeater | ||
- | |||
- | Source : http:// | ||
- | ===== GridView ===== | ||
- | < | ||
- | <code aspnet> | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | ReadOnly=" | ||
- | < | ||
- | </ | ||
- | </ | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | </ | ||
- | </ | ||
- | |||
- | <code aspnet> | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | <%# ((Etudiant)Container.DataItem).ToString() %> | ||
- | </ | ||
- | </ | ||
- | < | ||
- | < | ||
- | <%# Html.GetShortDate(((Etudiant)Container.DataItem).Naissance) %> | ||
- | </ | ||
- | </ | ||
- | < | ||
- | < | ||
- | <%# Eval(((Etudiant)Container.DataItem).Note.ToString(), | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | ==== Commande supprimer ==== | ||
- | Il peut être intéressant d' | ||
- | |||
- | Il faut d' | ||
- | <code asp|Ajout de la colonne supprimer au GridView> | ||
- | < | ||
- | < | ||
- | < | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | Ensuite, implémenter la méthode '' | ||
- | <code csharp|Méthode DeleteItem> | ||
- | protected void DeleteItem(object sender, CommandEventArgs e) | ||
- | { | ||
- | if (e.CommandArgument.ToString().Length > 0) | ||
- | { | ||
- | int id = Convert.ToInt32(e.CommandArgument.ToString()); | ||
- | Person personne = new Personne(id); | ||
- | personne.Delete(); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | ==== DataKeyNames ==== | ||
- | |||
- | <code asp> | ||
- | < | ||
- | </ | ||
- | |||
- | <code csharp> | ||
- | int Id = int.Parse(GridView1.DataKeys[e.NewEditIndex].Value.ToString()); | ||
- | </ | ||
- | |||
- | ==== Changer dynamiquement les entêtes de colonnes ==== | ||
- | < | ||
- | GridView1.Columns[0].HeaderText = "Nom de la colonne"; | ||
- | </ | ||
- | ==== Accéder aux contrôles ==== | ||
- | |||
- | <code csharp> | ||
- | foreach (GridViewRow gvr in gv.Rows) | ||
- | { | ||
- | CheckBox chk = (CheckBox)gvr.FindControl(" | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Événements du GridView ==== | ||
- | <code csharp> | ||
- | protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) | ||
- | { | ||
- | System.Diagnostics.Debug.WriteLine(" | ||
- | } | ||
- | protected void GridView1_DataBinding(object sender, EventArgs e) | ||
- | { | ||
- | System.Diagnostics.Debug.WriteLine(" | ||
- | } | ||
- | protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) | ||
- | { | ||
- | System.Diagnostics.Debug.WriteLine(" | ||
- | } | ||
- | protected void GridView1_DataBound(object sender, EventArgs e) | ||
- | { | ||
- | System.Diagnostics.Debug.WriteLine(" | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | * [[http:// | ||
- | |||
- | ==== Raccourcir une chaine ==== | ||
- | S'il y a une description, | ||
- | <code csharp> | ||
- | protected string ValidateString(object String) | ||
- | { | ||
- | if ((String.ToString().Length > 100)) | ||
- | { | ||
- | return String.ToString().Substring(0, | ||
- | } | ||
- | else | ||
- | { | ||
- | return String.ToString(); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Et dans le fichier ASPX : | ||
- | <code asp> | ||
- | < | ||
- | < | ||
- | <%# ValidateString(Container.DataItem(" | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | Source : http:// | ||
- | |||
- | \\ | ||
- | |||
- | Peut se faire autrement, lors du '' | ||
- | <code csharp> | ||
- | protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) | ||
- | { | ||
- | if (e.Item is GridDataItem) | ||
- | { | ||
- | GridDataItem dataBoundItem = e.Item as GridDataItem; | ||
- | if (dataBoundItem[" | ||
- | { | ||
- | dataBoundItem[" | ||
- | } | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | ==== Ressources ==== | ||
- | * [[http:// | ||
- | |||
- | ===== LinqDataSource ===== | ||
- | Un fournisseur de données qui utilise [[bd: | ||
- | <code asp> | ||
- | < | ||
- | ContextTypeName=" | ||
- | TableName=" | ||
- | GroupBy=" | ||
- | Select=" | ||
- | It As Products, | ||
- | Max(ListPrice) As MaxListPrice, | ||
- | Min(ListPrice) As MinListPrice)" | ||
- | ID=" | ||
- | runat=" | ||
- | </ | ||
- | </ | ||
- | |||
- | <code asp> | ||
- | Select=" | ||
- | </ | ||
- | |||
- | <code asp> | ||
- | < | ||
- | ContextTypeName=" | ||
- | EnableDelete=" | ||
- | EnableInsert=" | ||
- | EnableUpdate=" | ||
- | OrderBy=" | ||
- | TableName=" | ||
- | OnDeleted=" | ||
- | </ | ||
- | </ | ||
- | |||
- | Source : [[http:// | ||
- | |||
- | ===== UpdatePanel ===== | ||
- | ===== Hyperlink ===== | ||
- | Pour utiliser un hyperlien ASP.NET avec un '' | ||
- | <code asp> | ||
- | < | ||
- | |||
- | < | ||
- | NavigateUrl='< | ||
- | </ | ||
- | |||
- | ===== DropDownList ===== | ||
- | |||
- | ==== Sélection qui change ==== | ||
- | |||
- | Si un '' | ||
- | <code csharp> | ||
- | protected void Page_Load(object sender, EventArgs e) | ||
- | { | ||
- | if (!Page.IsPostBack) | ||
- | { //ajouter les éléments (binding) | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | ==== DataBind avec LINQ ==== | ||
- | |||
- | <code csharp> | ||
- | DropDownList ddlCompanyList = (DropDownList )PanelBar1.FindItemByValue(" | ||
- | ddlCompanyList .DataTextField = " | ||
- | ddlCompanyList .DataValueField = " | ||
- | ddlCompanyList .DataSource = from c in db.Company | ||
- | select new { c.CompanyID, | ||
- | ddlCompanyList .DataBind(); | ||
- | </ | ||
- | |||
- | Autre exemple avec la liaison de la valeur courante et la liste : | ||
- | |||
- | <code csharp> | ||
- | public List< | ||
- | { | ||
- | return (from e in db.Employees | ||
- | select e).ToList(); | ||
- | } | ||
- | |||
- | protected void rgGestionAcces_ItemCreated(object sender, GridItemEventArgs e) | ||
- | { | ||
- | // ... | ||
- | Employee employee = db.Employees.Single(e => e.IdEmployee == employeeId); | ||
- | cbxEmployes.DataValueField = " | ||
- | cbxEmployes.DataTextField = " | ||
- | cbxEmployes.DataSource = GetListeEmployes(db); | ||
- | cbxEmployes.DataBind(); | ||
- | cbxEmployes.SelectedValue = employee.IdEmployee.ToString(); | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | ====== Utilisation du JavaScript ====== | ||
- | ===== Ajout d' | ||
- | |||
- | Le JavaScript peut être utilisé de différentes façons, voici un exemple avec l' | ||
- | < | ||
- | protected void Page_Load(object sender, System.EventArgs e) | ||
- | { | ||
- | | ||
- | | ||
- | } | ||
- | </ | ||
- | |||
- | ====== Authentification Windows ====== | ||
- | Il est possible de d' | ||
- | <code xml> | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | < | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | Le ''?'' | ||
- | |||
- | |||
- | Pour aller chercher les informations dans la page : | ||
- | <code csharp> | ||
- | TextBox txtLogon = (TextBox)PanelBar1.FindItemByValue(" | ||
- | System.Security.Principal.IPrincipal user = HttpContext.Current.User; | ||
- | txtLogon.Text = user.Identity.Name; | ||
- | </ | ||
- | |||
- | ===== Avec Firefox ===== | ||
- | |||
- | Firefox supporte l' | ||
- | |||
- | ^ Paramètre ^ Statut | ||
- | | network.automatic-ntlm-auth.allow-proxies | ||
- | | network.automatic-ntlm-auth.trusted-uris | ||
- | |||
- | ===== Utilisation d' | ||
- | |||
- | [[os: | ||
- | <code csharp> | ||
- | System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent(); | ||
- | string[] a = Context.User.Identity.Name.Split(' | ||
- | |||
- | System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry(" | ||
- | txtName.Text = ADEntry.Properties[" | ||
- | </ | ||
- | |||
- | L' | ||
- | |||
- | ===== Ressources ===== | ||
- | * [[http:// | ||
- | * [[http:// | ||
- | * [[http:// | ||
- | |||
- | |||
- | ====== Web.config ====== | ||
- | |||
- | ===== Debugging mode ===== | ||
- | |||
- | Le mode debug peut être modifié de deux façons : | ||
- | - Dans '' | ||
- | < | ||
- | < | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | - Dans '' | ||
- | < | ||
- | < | ||
- | </ | ||
- | </ | ||
- | </ | ||
- | |||
- | ==== Accéder par programmation ==== | ||
- | |||
- | <code csharp> | ||
- | HttpContext context = HttpContext.Current; | ||
- | if (context.IsDebuggingEnabled) | ||
- | { | ||
- | // code | ||
- | } | ||
- | </ | ||
- | |||
- | <code csharp> | ||
- | using System.Configuration; | ||
- | using System.Web.Configuration; | ||
- | //... | ||
- | CompilationSection configSection = | ||
- | if (configSection.Debug) | ||
- | { | ||
- | // code | ||
- | } | ||
- | </ | ||
- | |||
- | |||
- | |||
- | ====== Gérer des données binaires ou dynamiques ====== | ||
- | |||
- | |||
- | ===== Générer une image dynamiquement ===== | ||
- | |||
- | Il est possible de générer une image à l'aide d'un HTTPHandler. | ||
- | <code asp> | ||
- | Bitmap objBitmap = new Bitmap(200, 200); | ||
- | Graphics objGraphics = Graphics.FromImage(objBitmap); | ||
- | |||
- | objGraphics.Clear(Color.White); | ||
- | Brush b1 = new SolidBrush(Color.Red); | ||
- | Rectangle rect = new Rectangle(10, | ||
- | objGraphics.FillRectangle(b1, | ||
- | |||
- | // Save the image to the OutputStream | ||
- | Response.ContentType = " | ||
- | objBitmap.Save(Response.OutputStream, | ||
- | | ||
- | objBitmap.Dispose(); | ||
- | objGraphics.Dispose(); | ||
- | </ | ||
- | |||
- | |||
- | ===== Générer dynamiquement des documents PDF ===== | ||
- | Créer un nouveau fichier ASPX et dans la fonction '' | ||
- | <code asp> | ||
- | protected void Page_Load(object sender, EventArgs e) | ||
- | { | ||
- | Response.Clear(); | ||
- | Response.ContentType = " | ||
- | MemoryStream m = new MemoryStream(); | ||
- | Document document = new Document(); | ||
- | PdfWriter writer = PdfWriter.GetInstance(document, | ||
- | document.Open(); | ||
- | |||
- | document.Add(new Paragraph(" | ||
- | document.Add(new Paragraph(" | ||
- | document.Close(); | ||
- | |||
- | writer.Flush(); | ||
- | Response.OutputStream.Write(m.GetBuffer(), | ||
- | Response.OutputStream.Flush(); | ||
- | Response.OutputStream.Close(); | ||
- | Response.End(); | ||
- | } | ||
- | </ | ||
- | Pour des exemple, voir les démonstrations d' | ||
- | |||
- | |||
- | |||
- | ===== Téléverser des données binaires ===== | ||
- | |||
- | Exemple pour téléverser (// | ||
- | |||
- | <code csharp> | ||
- | protected void btnUpload_Click(object sender, EventArgs e) | ||
- | { | ||
- | if (fuFileUploader.HasFile && fuFileUploader.PostedFile.ContentLength > 0) | ||
- | { | ||
- | string path_file_name = fuFileUploader.PostedFile.FileName; | ||
- | string ext = Path.GetExtension(path_file_name).Replace(" | ||
- | string file_name = Path.GetFileNameWithoutExtension(path_file_name); | ||
- | string file_title = txtFileTitle.Text.Trim(); | ||
- | HelperDataClassesDataContext db = new HelperDataClassesDataContext(); | ||
- | |||
- | try | ||
- | { | ||
- | byte[] file_byte = fuFileUploader.FileBytes; | ||
- | Linq.Binary file_binary = new Linq.Binary(file_byte); | ||
- | |||
- | ControlDocument cd = new ControlDocument | ||
- | { | ||
- | guid = Guid.NewGuid(), | ||
- | file_ext = ext, | ||
- | file_nm = file_name.Trim(), | ||
- | file_title=file_title, | ||
- | file_bin = file_binary, | ||
- | is_active = true, | ||
- | upload_page_type = rblLocation.SelectedValue, | ||
- | upload_dt = DateTime.Now, | ||
- | upload_by = UtilUniverse.Common.CurrentUserLogin.Trim() | ||
- | }; | ||
- | db.ControlDocuments.InsertOnSubmit(cd); | ||
- | } | ||
- | finally | ||
- | { | ||
- | db.SubmitChanges(); | ||
- | } | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Source : [[http:// | ||
- | \\ | ||
- | Voir aussi : http:// | ||
- | |||
- | * [[http:// | ||
- | |||
- | |||
- | ===== Télécharger des données binaires ===== | ||
- | |||
- | On peut créer un gestionnaire générique (//Generic handler//) dans le cas d'une utilisation avec [[web: | ||
- | <code csharp> | ||
- | using System.IO; | ||
- | |||
- | //... | ||
- | public void ProcessRequest(HttpContext context) | ||
- | { | ||
- | int id; | ||
- | if (int.TryParse(context.Request.Params[" | ||
- | CustomDataContext db = new CustomDataContext(); | ||
- | |||
- | Attachment attachment = db.Attachment.Single(a => a.IdAttachment == id); | ||
- | |||
- | FileInfo file = new FileInfo(attachment.FileName); | ||
- | context.Response.ContentType = GetContentType(file.Extension); | ||
- | context.Response.AddHeader(" | ||
- | context.Response.AddHeader(" | ||
- | context.Response.BinaryWrite(attachment.Data.ToArray()); | ||
- | context.Response.Flush(); | ||
- | } | ||
- | else { | ||
- | context.Response.ContentType = " | ||
- | context.Response.Write(" | ||
- | } | ||
- | } | ||
- | |||
- | private string GetContentType(string fileExtension) | ||
- | { | ||
- | switch (fileExtension) { | ||
- | case " | ||
- | case " | ||
- | return " | ||
- | case " | ||
- | return " | ||
- | case " | ||
- | return " | ||
- | case " | ||
- | return " | ||
- | default: | ||
- | return " | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | Si le fichier téléchargé est un type de fichier géré par le fureteur (ex: images, PDF, etc), ceux-ci s' | ||
- | <code csharp> | ||
- | Response.AppendHeader(" | ||
- | </ | ||
- | |||
- | ==== Limitation de la taille du fichier ==== | ||
- | Les fichiers qui sont téléversés vers le serveur avec le '' | ||
- | * Changer le fichier '' | ||
- | * Changer le '' | ||
- | |||
- | Mettre sous le node ''< | ||
- | |||
- | <code xml> | ||
- | < | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | | ||
- | </ | ||
- | |||
- | Il faudra peut-être changer aussi le paramètre '' | ||
- | |||
- | |||
- | == | ||
- | Sources : | ||
- | * [[http:// | ||
- | * [[http:// | ||
- | |||
- | |||
- | ====== Bouts de code ====== | ||
- | |||
- | ===== Détecter le navigateur ===== | ||
- | |||
- | <code csharp> | ||
- | private void Button1_Click(object sender, System.EventArgs e) | ||
- | { | ||
- | System.Web.HttpBrowserCapabilities browser = Request.Browser; | ||
- | string s = " | ||
- | + "Type = " | ||
- | + "Name = " | ||
- | + " | ||
- | + "Major Version = " | ||
- | + "Minor Version = " | ||
- | + " | ||
- | + "Is Beta = " | ||
- | + "Is Crawler = " | ||
- | + "Is AOL = " | ||
- | + "Is Win16 = " | ||
- | + "Is Win32 = " | ||
- | + " | ||
- | + " | ||
- | + " | ||
- | + " | ||
- | + " | ||
- | browser.EcmaScriptVersion.ToString() + " | ||
- | + " | ||
- | + " | ||
- | + " | ||
- | TextBox1.Text = s; | ||
- | } | ||
- | </ | ||
- | |||
- | Source : %%http:// | ||
- | |||
- | ===== Contrôles de validation ===== | ||
- | <code asp> | ||
- | < | ||
- | < | ||
- | </ | ||
- | |||
- | ====== Codes sources externes ====== | ||
- | * {{: | ||
- | |||
- | ====== Collections de composants ====== | ||
- | * [[http:// | ||
- | * [[http:// | ||
- | * [[http:// | ||
- | * [[http:// | ||
- | * [[http:// | ||
- | |||
- | |||
- | ====== Bonnes pratiques ====== | ||
- | * [[http:// | ||
- | * [[http:// | ||
- | * [[http:// | ||
web/asp.net/toc.1392168388.txt.gz · Dernière modification : 2022/02/02 00:43 (modification externe)