Preferred Language:
Listing 9.28 - PhotoGallery.aspx
Illustrates how to execute the SqlDataSource Select command programmatically.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Photo Gallery</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList
id="dlstImages"
DataSourceID="srcImages"
RepeatColumns="3"
Runat="server">
<ItemTemplate>
<asp:Image ID="Image1"
ImageUrl='<%# String.Format("DynamicImage.ashx?id={0}", Eval("Id")) %>'
Width="250"
Runat="server" />
<br />
<%# Eval("Description") %>
</ItemTemplate>
</asp:DataList>
<hr />
<asp:FormView
id="frmImage"
DataSourceID="srcImages"
DefaultMode="Insert"
Runat="server">
<InsertItemTemplate>
<asp:Label
id="lblImage"
Text="Upload Image:"
AssociatedControlId="upImage"
Runat="server" />
<br />
<asp:FileUpload
id="upImage"
FileBytes='<%# Bind("Image") %>'
Runat="server" />
<br /><br />
<asp:Label
id="lblDescription"
Text="Description:"
AssociatedControlID="txtDescription"
Runat="server" />
<br />
<asp:TextBox
id="txtDescription"
Text='<%# Bind("Description") %>'
TextMode="MultiLine"
Columns="50"
Rows="2"
Runat="server" />
<br /><br />
<asp:Button
id="btnInsert"
Text="Add Image"
CommandName="Insert"
Runat="server" />
</InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSource
id="srcImages"
SelectCommand="SELECT ID,Description FROM Images"
InsertCommand="INSERT Images (Image,Description)
VALUES (@Image,@Description)"
ConnectionString="<%$ ConnectionStrings:Images %>"
Runat="server" />
</div>
</form>
</body>
</html>
The following generic handler is used to display the images.
<%@ WebHandler Language="C#" Class="DynamicImage" %>
using System.Data;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
/// <summary>
/// Displays an image corresponding to the Id passed
/// in a query string field
/// </summary>
public class DynamicImage : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
// Get the Id of the image to display
string imageId = context.Request.QueryString["Id"];
// Use SqlDataSource to grab image bytes
SqlDataSource src = new SqlDataSource();
src.ConnectionString = WebConfigurationManager.ConnectionStrings["Images"].ConnectionString;
src.SelectCommand = "SELECT Image FROM Images WHERE Id=" + imageId;
// Return a DataView
DataView view = (DataView)src.Select(DataSourceSelectArguments.Empty);
context.Response.BinaryWrite( (byte[])view[0]["Image"]);
// Return a DataReader
//src.DataSourceMode = SqlDataSourceMode.DataReader;
//IDataReader reader = (IDataReader)src.Select(DataSourceSelectArguments.Empty);
//reader.Read();
//context.Response.BinaryWrite((byte[])reader["Image"]);
//reader.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}
Need ASP.NET and Visual Studio 2008 Training?
- Learn ASP.NET 3.5 from Stephen Walther, author of ASP.NET 3.5 Unleashed.
We've provided ASP.NET training for NASA, Lockheed Martin, the National Science Foundation, Verizon,
Boeing, the US House of Representatives, Kaiser, Petco, Mary Kay, and Microsoft.
Why not your company?
-
Receive a four day, hands-on, intensive workshop.
-
We fly to you, anywhere in the world.
-
We can bring our own laptops.
To learn more, visit the
Superexpert Training website.