´ËʾÀýÑÝʾÁËÔÚASP.NET FormViewÖÐÈçºÎÉÏ´«ºÍÏÔʾͼƬ£¬ÈçºÎÌí¼Ó£¬±à¼£¬É¾³ýºÍ·ÖÒ³¡£
FormViewĬÈÏÏÔʾÁË´ÓÊý¾Ý¿âÖÐÈ¡³öµÄÊý¾ÝÒÔ¼°»ù±¾²Ù×÷µÄ½çÃæ
ͼƬÊÇ´ÓSQL Server Êý¾Ý¿âÖÐÈ¡³ö£¬²¢ÏÔʾÔÚÍøÒ³ÉÏÃæ¡£
ʵÏÖ²½Ö裺
²½Öè1£ºÔÚVisual Studio Öд´½¨WebÓ¦ÓóÌÐòÏîÄ¿CSASPNETFormViewUpload¡£
²½Öè2£ºÍÏ×§FormView¿Ø¼þµ½Default.aspxÒ³Ãæ¡£
(1) ÖØÃüÃûFormViewΪfvPerson¡£
(2) ÔÚÏÖÔڵĴúÂëÖи´ÖÆÕ³ÌùÏÂÃæµÄÄ£°æ±êÇ©£º
ItemTemplate£ºÖض¨ÒåFormViewµÄ¼Ç¼ÏÔʾ¡£
EditItemTemplate£º×Ô¶¨Òå±à¼ÊÓͼ
InsertItemTemplate£º×Ô¶¨Òå²åÈëÊÓͼ
²½Öè3£º´ÓʾÀý´úÂë¸´ÖÆÕ³ÌùÏÂÃæµÄ·½·¨µ½ÎÒÃǵÄÏîÄ¿ÎļþDefault.aspx.cs
Page_Load·½·¨£ºµ±Ò³Ãæ¼ÓÔØµÄʱºò³õʼ»¯¶ÔÏó¡£
BindFormView·½·¨£º°ó¶¨FormView¿Ø¼þºÍSQL ServerÖÐµÄ±í¡£
²½Öè4£º´ò¿ªfvPersonµÄÊôÐÔÃæ°å£¬Çл»µ½Ê¼þ¡£Ë«»÷ÏÂÁÐʼþÉú³Éʼþ´¦Àí³ÌÐò¡£È»ºó£¬°´ÕÕʾÀýËùʾ´úÂëÍê³Éʼþ´¦Àí³ÌÐò¡£
(1) ModeChanging ʼþ£ºµ±FormViewÔÚEdit£¬InsertºÍReadOnlyģʽ֮¼äÇл»Ê±£¬ÖØÐ°ó¶¨FormView¿Ø¼þ£¬ÒÔ±ãÔÚеÄģʽÏÂÏÔʾÊý¾Ý¡£
////////////////////////////////////////////////////////////////
fvPerson.ChangeMode(e.NewMode);
BindFormView();
////////////////////////////////////////////////////////////////
(2) PageIndexChangingʼþ£ºµ±µ¥»÷·Ò³°´Å¥µÄʱºòÒý·¢´Ëʼþ¡£ÎªÁËÔÚеÄÒ³ÃæÏÔʾµÄÊý¾Ý£¬ÎÒÃÇÐèÒªÉèÖÃеÄÒ»Ò³µÄË÷Òý£¬È»ºóÖØÐ°ó¶¨FormView¿Ø¼þ¡£
////////////////////////////////////////////////////////////////
fvPerson.PageIndex = e.NewPageIndex;
BindFormView();
////////////////////////////////////////////////////////////////
(3) ItemInserting ʼþ£ºµ±²åÈë°´Å¥±»µ¥»÷ÊÇÒý·¢£¬µ«ÊÇ´¦ÀíµÄÊDzåÈëÊý¾Ý֮ǰµÄ²Ù×÷¡£µ±µ¥»÷°´Å¥ºó£¬ÎÒÃÇÐèÒª´ÓInsertItemTemplateµÄFormView¿Ø¼þÖлñÈ¡first name,last nameºÍÖ¸¶¨Í¼Æ¬Îļþ(bytes)¡£
////////////////////////////////////////////////////////////////
string strLastName = ((TextBox)fvPerson.Row.FindControl("tbLastName")).Text;
string strFirstName = ((TextBox)fvPerson.Row.FindControl("tbFirstName")).Text;
cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 50).Value = strLastName;
cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50).Value = strFirstName;
FileUpload uploadPicture = (FileUpload)fvPerson.FindControl("uploadPicture");
if (uploadPicture.HasFile)
{
cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = uploadPicture.FileBytes;
}
else
{
cmd.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = DBNull.Value;
}
////////////////////////////////////////////////////////////////
(4) ItemUpdating ʼþ£ºµ±µ¥»÷¸üа´Å¥µÄʱºòÒý·¢µÄʼþ¡£µ«ÊÇ´¦ÀíµÄÊÇÔÚ¸üÐÂÊý¾Ý֮ǰµÄ²Ù×÷¡£µ±µ¥»÷¸üа´Å¥ºó£¬ÎÒÃÇÐèÒª´ÓEditItemTemplateÖеÄFormView¿Ø¼þ»ñÈ¡ºÍ´«µÝperson ID,first name, last nameºÍÖ¸¶¨µÄͼÏñÎļþ(bytes)¡£
////////////////////////////////////////////////////////////////
string strPersonID = ((Label)fvPerson.Row.FindControl("lblPersonID")).Text;
////////////////////////////////////////////////////////////////
²½Öè5£º´´½¨Ò»¸öеÄASP.NET Handle´¦Àí³ÌÐòImageHandler.ashx£¬ÓÃÀ´´¦Àí´ÓÊý¾Ý¿âÖлñȡͼƬÎļþ²¢Êä³öµ½ÍøÒ³¡£
///
/// Connected to the DataBase,
/// If the specific record has image,read out the specific row's
/// image byte collection as well as image type, output it.
/// If not, output the default image instead.
///
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = new SqlConnection(
ConfigurationManager.ConnectionStrings["db_PersonsConnectionString"].ConnectionString);
cmd.Connection.Open();
cmd.CommandText = "select PersonImage,PersonImageType from tb_personInfo" +