主要完成的功能是:使图片上传控件可以像textbox控件那样在detailsview控件中使用,
以下为代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel.Design;
namespace WebControlLibrary
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:UploadImg runat=server></{0}:UploadImg>")]
public class UploadImg :CompositeControl
{
private Button _button;
private Image _image;
private FileUpload _fileUpload;
private Label LabMessage1;
private string _uploadImgImageUrl="~/img/net/not.gif";
private static readonly object EventSubmitKey = new object();
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string UploadImgImageUrl
{
get
{
EnsureChildControls();
String s = (String)ViewState["UploadImgImageUrl"];
return ((s == null) ? _uploadImgImageUrl : s);
}
set
{
EnsureChildControls();
ViewState["UploadImgImageUrl"] = value;
_image.ImageUrl= value;
}
}
public event EventHandler Submit
{
add
{
Events.AddHandler(EventSubmitKey, value);
}
remove
{
Events.RemoveHandler(EventSubmitKey,value);
}
}
protected virtual void OnSubmit(EventArgs e)
{
EventHandler SubmitHandler = (EventHandler) Events[EventSubmitKey];
if (SubmitHandler != null) {
SubmitHandler(this, e);
}
}
// 实现Submit事件引发的事件处理程序
private void _button_Click(Object source, EventArgs e)
{
OnSubmit(EventArgs.Empty);
bool fileOK = false;
string path =Page.Server.MapPath("~/img/guest/");
if (_fileUpload.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(_fileUpload.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
if (fileOK)
{
try
{
DateTime dt = DateTime.Now;
string theFileName = dt.Second + dt.Month + dt.Day + dt.DayOfYear + _fileUpload.FileName;
_fileUpload.SaveAs(path + theFileName);
UploadImgImageUrl = "~/img/guest/" + theFileName;
LabMessage1.Text = "文件上传成功.";
}
catch (Exception ex)
{
LabMessage1.Text = "文件上传不成功.";
}
}
else
{
LabMessage1.Text = "只能够上传图片文件.";
}
}
// 重写ICompositeControlDesignerAccessor接口的RecreateChildContrls方法
protected override void RecreateChildControls() {
EnsureChildControls();
}
//重写CreateChildControls方法,将子控件添加到复合控件中
protected override void CreateChildControls()
{
Controls.Clear();
_button = new Button();
_image = new Image();
_fileUpload = new FileUpload();
LabMessage1 = new Label();
_button.ID = "UploadImgbtn";
_button.Text = "上传";
_button.Click += new EventHandler(_button_Click);
_image.Width = 100;
_image.Height = 80;
_image.ImageUrl = UploadImgImageUrl;
this.Controls.Add(_button);
this.Controls.Add(_image);
this.Controls.Add(_fileUpload);
this.Controls.Add(LabMessage1);
}
//重写Render方法,呈现控件中其他的HTML代码
protected override void Render(HtmlTextWriter output) {
output.AddAttribute(HtmlTextWriterAttribute.Border, "0px");
output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "5px");
output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0px");
output.RenderBeginTag(HtmlTextWriterTag.Table);
output.RenderBeginTag(HtmlTextWriterTag.Tr);
output.RenderBeginTag(HtmlTextWriterTag.Td);
_image.RenderControl(output);
output.RenderEndTag();
output.RenderBeginTag(HtmlTextWriterTag.Td);
_fileUpload.RenderControl(output);
_button.RenderControl(output);
output.RenderEndTag();
output.RenderEndTag();
output.RenderBeginTag(HtmlTextWriterTag.Tr);
output.RenderBeginTag(HtmlTextWriterTag.Td);
LabMessage1.RenderControl(output);
output.RenderEndTag();
output.RenderEndTag();
output.RenderEndTag();
}
}
}