原理:修改生成的html中的input type=file控件的属性,隐藏其中文本框,加上一个文本框,实现变相的上传控件。
其生成的html原代码为:
<input id='uploadText' value=''/> <input onpropertychange="uploadText.value=this.value;" type="file" name="MyUploadFile1" id="MyUploadFile1" style="width:0px;border-width:0" />
其中红色就为其中的关键,相信大家都看的懂吧,我就不再解释了
呵呵(只改了一下显示其实质还是原来的fileupload).
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
namespace LK.WebControl
{
/// <summary>
/// MyUploadFile 的摘要说明
/// </summary>
public class MyUploadFile : FileUpload
{
public MyUploadFile()
{
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:0px;border-width:0");
writer.AddAttribute("onpropertychange", "uploadText.value=this.value;");
base.AddAttributesToRender(writer);
}
#region 属性
/// <summary>
/// 文本框与命令按钮的间距
/// </summary>
[BrowsableAttribute(true)]
[DescriptionAttribute("文本框与命令按钮的间距")]
[CategoryAttribute("外观")]
public int CellSpace
{
get { return _cellSpace; }
set { _cellSpace = value; }
}
private int _cellSpace = 0;
[BrowsableAttribute(true)]
[DescriptionAttribute("文本框的样式")]
[CategoryAttribute("外观")]
public string TextCss
{
get { return _textCss; }
set { _textCss = value; }
}
private string _textCss;
[BrowsableAttribute(true)]
[DescriptionAttribute("文本框的宽度")]
[CategoryAttribute("外观")]
public string TextWidth
{
get { return _textWidth; }
set { _textWidth = value; }
}
private string _textWidth;
[BrowsableAttribute(true)]
[DescriptionAttribute("文本框的高度")]
[CategoryAttribute("外观")]
public string TextHeight
{
get { return _textHeight; }
set { _textHeight = value; }
}
private string _textHeight;
#endregion
public override void RenderControl(HtmlTextWriter writer)
{
if (Visible)
{
string strStyle = "style='{0}'";
string styleValue = "";
writer.Write("<input id='uploadText' value='' ");
if (!string.IsNullOrEmpty(_textWidth))
styleValue+= "width:"+_textWidth+"px;";
if (!string.IsNullOrEmpty(_textHeight))
styleValue +="height:"+_textHeight+ "px;";
if (!string.IsNullOrEmpty(_textCss))
writer.Write(" class='" + _textCss + "' ");
if (!string.IsNullOrEmpty(styleValue))
styleValue = string.Format(strStyle,styleValue);
writer.Write(styleValue );
writer.Write(" />");
for (int i = 0; i < _cellSpace; i++)
{
writer.Write(" ");
}
}
base.RenderControl(writer);
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
namespace LK.WebControl
{
/// <summary>
/// MyUploadFile 的摘要说明
/// </summary>
public class MyUploadFile : FileUpload
{
public MyUploadFile()
{
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:0px;border-width:0");
writer.AddAttribute("onpropertychange", "uploadText.value=this.value;");
base.AddAttributesToRender(writer);
}
#region 属性
/// <summary>
/// 文本框与命令按钮的间距
/// </summary>
[BrowsableAttribute(true)]
[DescriptionAttribute("文本框与命令按钮的间距")]
[CategoryAttribute("外观")]
public int CellSpace
{
get { return _cellSpace; }
set { _cellSpace = value; }
}
private int _cellSpace = 0;
[BrowsableAttribute(true)]
[DescriptionAttribute("文本框的样式")]
[CategoryAttribute("外观")]
public string TextCss
{
get { return _textCss; }
set { _textCss = value; }
}
private string _textCss;
[BrowsableAttribute(true)]
[DescriptionAttribute("文本框的宽度")]
[CategoryAttribute("外观")]
public string TextWidth
{
get { return _textWidth; }
set { _textWidth = value; }
}
private string _textWidth;
[BrowsableAttribute(true)]
[DescriptionAttribute("文本框的高度")]
[CategoryAttribute("外观")]
public string TextHeight
{
get { return _textHeight; }
set { _textHeight = value; }
}
private string _textHeight;
#endregion
public override void RenderControl(HtmlTextWriter writer)
{
if (Visible)
{
string strStyle = "style='{0}'";
string styleValue = "";
writer.Write("<input id='uploadText' value='' ");
if (!string.IsNullOrEmpty(_textWidth))
styleValue+= "width:"+_textWidth+"px;";
if (!string.IsNullOrEmpty(_textHeight))
styleValue +="height:"+_textHeight+ "px;";
if (!string.IsNullOrEmpty(_textCss))
writer.Write(" class='" + _textCss + "' ");
if (!string.IsNullOrEmpty(styleValue))
styleValue = string.Format(strStyle,styleValue);
writer.Write(styleValue );
writer.Write(" />");
for (int i = 0; i < _cellSpace; i++)
{
writer.Write(" ");
}
}
base.RenderControl(writer);
}
}
}