NiTrOuS Posted November 20, 2006 Share Posted November 20, 2006 Bonjour, voilà j'ai un gros soucis que je n'ai toujours pas résolu. J'ai une page web en ASP.NET nommée index.aspx J'utilise du code C# en codebehind donc, pour faire une requête SQL (procédure stockée) dans SQL Server 2005 Ma page d'index s'affiche bien, le codebehind aussi (j'affiche la réponse de ma requête avec System.Diagnostics.Debug.Writeline... Mais je voudrais en fait intégrer la réponse de mon code C# dans ma page web... Et je ne sais pas du tout comment faire :( Voici mes deux fichiers: index.aspx <%@ Page language="c#" Codebehind="index.aspx.cs" AutoEventWireup="false" Inherits="FoldersAndDocumentsNavigation.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm1</title> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"> <meta content="C#" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="FolderForm" runat="server"> <div align="center"> <h1><u>Folders and Documents Navigation</u></h1> <hr> <u>Recherche</u> <br> <hr> <u>Navigation</u> <br> <table width="100%"> <tr> <td align="left"><a href="index.aspx">Root</a></td> <td align="right"><asp:button id="ButtonAdd" Text="Ajouter" runat="server"></asp:button></td> </tr> </table> <IMG src="dossier.gif"> <hr> <u>Gestion des documents</u> <br> </div> </form> </body> </HTML> Et index.aspx.cs using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.IO; using System.Data.SqlClient; using System.Configuration; using System.Diagnostics; namespace FoldersAndDocumentsNavigation { /// <summary> /// Summary description for WebForm1. /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Button ButtonAdd; protected System.Web.UI.HtmlControls.HtmlForm FolderForm; private void Page_Load(object sender, System.EventArgs e) { string storedProcedure = "ReadingFolders"; try { //Déclaration de la connexion à la DB en allant chercher les paramètres dans le fichier de configuration SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionParameter"]); //Déclaration de la fonction à exécuter SqlCommand command = new SqlCommand(storedProcedure,connection); //Déclaration du type de fonction à exécuter command.CommandType = CommandType.StoredProcedure; connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { System.Diagnostics.Debug.WriteLine(reader.GetString(0)); } reader.Close(); connection.Close(); } catch (Exception ex) { Console.WriteLine("L'erreur suivante a été rencontrée :" + ex.Message); } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.ButtonAdd.Click += new System.EventHandler(this.ButtonAdd_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void ButtonAdd_Click(object sender, System.EventArgs e) { Response.Redirect("publish.aspx"); } } } Un grand merci d'avance à celui qui pourra me dépanner Je veux que ca s'exécute dans le onload private void Page_Load(object sender, System.EventArgs e) Link to comment Share on other sites More sharing options...
astero-H Posted November 20, 2006 Share Posted November 20, 2006 A la base j'y connais rien du tout mais vu que je travaille pas mal avec .net et que je suis curieux... J'ai trouvé un p'tit exemple à la con pour le principe... http://www.ondotnet.com/pub/a/dotnet/2001/...rms.html?page=5 Donc moi je verrais bien un objet du style <asp:DataGrid id=DataGrid1 ... /> dans index.aspx Dans ta classe c# tu as donc un objet DataGrid que tu remplis sur le PageLoad Link to comment Share on other sites More sharing options...
NiTrOuS Posted November 21, 2006 Author Share Posted November 21, 2006 J'ai trouvé un exemple qui m'irait parfaitement, mais il ne fonctionne pas :( http://www.aspfr.com/code.aspx?ID=9028 J'ai fait comme ceci: <HTML> <HEAD> <title>WebForm1</title> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"> <meta content="C#" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="FolderForm" runat="server"> <div align="center"> <h1><u>Folders and Documents Navigation</u></h1> <hr> <u>Recherche</u> <br> <hr> <u>Navigation</u> <br> <table width="100%"> <tr> <td align="left"><A href="index.aspx">Root</A></td> <td align="right"><asp:button id="ButtonAdd" runat="server" Text="Ajouter"></asp:button></td> </tr> </table> <IMG src="dossier.gif"> <hr> <u>Gestion des documents</u> <br> <asp:Table id="Table1" runat="server"> </asp:Table> </div> </form> </body> </HTML> private void Page_Load(object sender, System.EventArgs e) { string storedProcedure = "ReadingFolders"; try { HtmlTableRow Maligne = new HtmlTableRow(); HtmlTableCell MaCase = new HtmlTableCell(); MaCase.InnerText = "TEST"; Maligne.Cells.Add(MaCase); Table1.Rows.Add(Maligne); //Déclaration de la connexion à la DB en allant chercher les paramètres dans le fichier de configuration SqlConnection connection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionParameter"]); //Déclaration de la fonction à exécuter SqlCommand command = new SqlCommand(storedProcedure,connection); //Déclaration du type de fonction à exécuter command.CommandType = CommandType.StoredProcedure; connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { System.Diagnostics.Debug.WriteLine(reader.GetString(0)); } reader.Close(); connection.Close(); } catch (Exception ex) { Console.WriteLine("L'erreur suivante a été rencontrée :" + ex.Message); } } Il me met que j'ai une erreur à la ligne Table1.Rows.Add(Maligne); Il met: c:\inetpub\wwwroot\FoldersAndDocumentsNavigation\index.aspx.cs(36): The best overloaded method match for 'System.Web.UI.WebControls.TableRowCollection.Add(System.Web.UI.WebControls.TableRow)' has some invalid arguments Link to comment Share on other sites More sharing options...
NiTrOuS Posted November 21, 2006 Author Share Posted November 21, 2006 C'est bon j'ai trouvé. Alors il faut: Déclarer la table dans le fichier index.aspx <table id="MonTableau" runat="server"> </table> Déclarer dans index.aspx.cs protected System.Web.UI.HtmlControls.HtmlTable MonTableau; Ajouter les lignes et colonnes: HtmlTableRow Maligne = new HtmlTableRow(); HtmlTableCell MaCase = new HtmlTableCell(); MaCase.InnerText = "TEST"; Maligne.Cells.Add(MaCase); MonTableau.Rows.Add(Maligne); Pour insérer du code HTML et qu'il soit interprété, il suffit de changer InnerText en InnerHtml: HtmlTableRow Maligne = new HtmlTableRow(); HtmlTableCell MaCase = new HtmlTableCell(); MaCase.InnerHtml = "<IMG src=dossier.gif>"; Maligne.Cells.Add(MaCase); MonTableau.Rows.Add(Maligne); Merci quand même pour l'aide Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.