Simple ASP.NET Output Question
-
Although competent in C#, I'm new at ASP.NET, so this one's got me stumped. I've build a basic aspx page, and I want to write HTML lines to it programmatically through the Page_Load event handler. Here's the code:
private void Page_Load(object sender, System.EventArgs e) { SqlCommand cmFAQs = new SqlCommand("SELECT Question, Answer FROM tblWebFAQsQuestion", this.cnFAQs); this.cnFAQs.Open(); SqlDataReader rdrFAQs = cmFAQs.ExecuteReader(CommandBehavior.CloseConnection); int i = 0; while (rdrFAQs.Read()) { this.Page.Response.Output.WriteLine(" "); this.Page.Response.Output.WriteLine(rdrFAQs.GetString(0)); this.Page.Response.Output.WriteLine(" "); this.Page.Response.Output.WriteLine(" "); this.Page.Response.Output.WriteLine("**Answer:** "); this.Page.Response.Output.WriteLine(rdrFAQs.GetString(1)); this.Page.Response.Output.WriteLine(" "); i++; } rdrFAQs.Close(); }
It works great, except one thing - which will be obvious to any veteran. The WriteLine method outputs the line to the beginning of the HTML page. I need the output to be placed inside the of the page so I can build a form around it. Is there a (hopefully simple) way to do this? Thanks for your help! -
Although competent in C#, I'm new at ASP.NET, so this one's got me stumped. I've build a basic aspx page, and I want to write HTML lines to it programmatically through the Page_Load event handler. Here's the code:
private void Page_Load(object sender, System.EventArgs e) { SqlCommand cmFAQs = new SqlCommand("SELECT Question, Answer FROM tblWebFAQsQuestion", this.cnFAQs); this.cnFAQs.Open(); SqlDataReader rdrFAQs = cmFAQs.ExecuteReader(CommandBehavior.CloseConnection); int i = 0; while (rdrFAQs.Read()) { this.Page.Response.Output.WriteLine(" "); this.Page.Response.Output.WriteLine(rdrFAQs.GetString(0)); this.Page.Response.Output.WriteLine(" "); this.Page.Response.Output.WriteLine(" "); this.Page.Response.Output.WriteLine("**Answer:** "); this.Page.Response.Output.WriteLine(rdrFAQs.GetString(1)); this.Page.Response.Output.WriteLine(" "); i++; } rdrFAQs.Close(); }
It works great, except one thing - which will be obvious to any veteran. The WriteLine method outputs the line to the beginning of the HTML page. I need the output to be placed inside the of the page so I can build a form around it. Is there a (hopefully simple) way to do this? Thanks for your help! -
Put the html code in a string (use a StringBuilder) and put the string in the InnerHtml property of a place holder (a div tag with runat="server") on the page. Use quotes instead of apostrophes in the html code. --- b { font-weight: normal; }