you are correct in that a gridview is not the way to go about this. the gridview is meant to display editable, tabular data. if that's not what you data is, then a gridview is not correct control to bind to.
webforms is a html engine. it takes data (typically from a database) and puts the data (the dynamic part) into the html template (the static part). once this completes webforms writes the result to the response stream. that steam is sent to the client and the client can render the html in the browser.
something else to note. webforms is an html rendering engine. asp.net is an http framework. they are 2 different frameworks with 2 different purposes. webforms is built on top of asp.net, but asp.net does not require webforms. I say this because it's a common misconception that
1. you must use webforms to develop webpage with .net
2. devs often think they are the same thing.
on to your situation...
you already have the html, it's stored in the database. so, for this portion of the application you don't need webforms, because there is nothing dynamic. you are simply returning text in the database to the client.
an IHttpHandler is part of the asp.net framework. a generic httphander (ashx) will automatically be handled by asp.net without any configuration.
you could also build your own handler and then map an extension to the handler in the web.config and IIS, but that is more complicated than it needs to be and I digress...
for this situation I recommend a generic http handler (ashx). add a new generic handler to your web project and name it [tt]RenderedCachedHtml[/tt]. then copy the members from above into the class. here are the key features.
1. the handler expects the key [tt]id[/tt] in the params collection. it can be in the querystring, the form, a cookie the items collection. You could be specific about pulling the value from the querystring, but prefer to abstract where the value comes from. Params is the next best thing.
you will want to add error handling in case the request is missing the id property. you may also want to name the property something a bit more descriptive.
2. Reuse return true. the handler is stateless, therefore asp.net can cache the handler which is a small preformance boost. no single user would notice a difference, but a high traffic site would capitalize on this from an overall preformance stand point.
3. once the text is retrieved from the database write the text to the response stream. the code may not be 100%, but it's the general idea. I also set the content type explicitly. this will tell the client what type of data it is. other options are xml, pain text, json, image, a specific application like word, excel, pdf)
comments on your code. there are number of problems with your data access code.
objects are not properly disposed.
there is no excuse for sql injection. a parameterized query solves many problems.
since the query is just returning a single string you could execute a scalar query.
at a minimum I would change the code to this
Code:
var connectionsettings = ConfigurationManager.ConnectionStrings["key in web.config"];
var factory = DbProviderFactories.GetFactory(connectionsettings .ProviderName);
using(var connection = factory.CreateConnection())
{
connection.ConnectionString = connectionsettings .ConnectionString;
using(var command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = "select [html] from [table] where [id] = @id";
var parameter = command.CreateParameter();
parameter.ParameterName = "id";
parameter.Value = id; //retrieved from the querystring
command.Parameters.Add(parameter);
connection.Open();
return command.ExecuteScalar();
}
}
for more information on what's happening, and why, read my FAQ on database connection management. the link is in my signature.
...
additional information. not directly related, but may help you understand what a webform is:
webforms (aspx) is another type of http handler, albeit much more complicated. from the [tt]Process(HttpContext)[/tt] member all the objects required for webforms to work. it's at this point a webform http hanlder will parse the Params collection and create the page life cycle. this allows the developer to work with a web page in a similar manner to how a winform works.
Jason Meckley
Programmer
faq855-7190
faq732-7259