第一个问题,可能是没有上传文件导致出错,但也不排除其它情况,不过没有代码无法知道;
第二个问题,没有用是什么意思?试着加上[Browsable(true)]属性。--------------------------------
AspNetPager 免费分页控件4.1版发布,欢迎下载使用:http://www.webdiyer.com
第二个问题,没有用是什么意思?试着加上[Browsable(true)]属性。--------------------------------
AspNetPager 免费分页控件4.1版发布,欢迎下载使用:http://www.webdiyer.com
<P>
<INPUT id="UploadFile" type="file" name="UploadFile" runat="server"><FONT face="宋体">
</FONT>
<asp:Button id="btnUpload" Text="上传" runat="server"></asp:Button>
<asp:Label id="lblShow" runat="server"></asp:Label></P>
<P><FONT face="宋体">图片地址:</FONT>
<asp:TextBox id="txtFileUrl" runat="server" Width="203px"></asp:TextBox></P>/*****************************************************/分离代码:
namespace myctrl
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel; /// <summary>
/// myCtrl 的摘要说明。
/// </summary>
/*[DefaultEvent("btnUpload_Click")]
[DefaultProperty("Dirname")]*/
public class myCtrl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Button btnUpload;
protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;
protected System.Web.UI.WebControls.Label lblShow;
private string filetype;//可上传文件类型
protected System.Web.UI.WebControls.TextBox txtFileUrl;
private long maxfilelength;//可上传文件最大大小
private string dirname;//上传到的目录的相对路径
public myCtrl()
{
filetype="jpg,bmp,zip,rar";
maxfilelength=500;
dirname="uploadfile";
}
private void btnUpload_Click(object sender, System.EventArgs e)
{
bool Ispost=true;
string PostfixList=this.FileType; //可上传文件类型
int intLoc;
string strPath=Server.MapPath(this.Dirname) + "\\";
string strFile1=this.UploadFile.PostedFile.FileName;
string SingleFileName,strPostfix,strNewFileName;
intLoc=strFile1.LastIndexOf('\\');
SingleFileName=strFile1.Substring(intLoc+1); //取单个文件名
intLoc=SingleFileName.LastIndexOf(".");
strPostfix=SingleFileName.Substring(intLoc+1); //去文件后缀
if(UploadFile.Value!="")
{
if(UploadFile.PostedFile.ContentLength==0 || UploadFile.PostedFile.ContentLength>this.MaxFileLength*1000) //如果文件大小等于0或者大于最大值
{
Ispost=false;
lblShow.Text="文件大小等于0或者大于" + this.MaxFileLength + "K,不能上传";
}
if(PostfixList.IndexOf(strPostfix)==-1)
{
Ispost=false;
lblShow.Text="文件类型只能是" + PostfixList;
}
}
else
{
Ispost=false;
lblShow.Text="请选择上传文件名";
}
if(Ispost)
{
/*files myfile=new files();
myfile.Filelen =file.PostedFile.ContentLength;
strNewFileName=DateTime.Now.DayOfYear.ToString().Replace("-","") + myfile.Filelen + SingleFileName;
myfile.Filename=strPath + strNewFileName;
myfile.Uploadtime=DateTime.Now.ToString();
file.PostedFile.SaveAs(myfile.Filename);
myfile.AddFile();*/
strNewFileName=DateTime.Now.DayOfYear.ToString().Replace("-","") + UploadFile.PostedFile.ContentLength + SingleFileName;
UploadFile.PostedFile.SaveAs(strNewFileName);
lblShow.Text="<b>上传成功!</b>";
txtFileUrl.Text=this.dirname + "//" + strNewFileName;
} } private void Page_Load(object sender, System.EventArgs e)
{
}
[DefaultValue("jpg,bmp,zip,rar")]
[Category("杂项"),Description("可上传文件类型")]
public string FileType
{
get
{
return filetype;
}
set
{
filetype=value;
}
}
[DefaultValue(500)]
[Category("杂项"),Description("可上传文件最大大小,以K为单位")]
public long MaxFileLength
{
get
{
return maxfilelength;
}
set
{
maxfilelength=value;
}
}
[DefaultValue("uploadfile")]
[Category("杂项"),Description("上传到的目录的相对路径")]
public string Dirname
{
get
{
return dirname;
}
set
{
dirname=value;
}
} }
}
AspNetPager 免费分页控件4.1版发布,欢迎下载使用:http://www.webdiyer.com
都不加?第一个问题如何解决?
encType="multipart/form-data"
<form id="aaa" method="post" runat="server" encType="multipart/form-data">
第一个问题,你的代码把IDE自动添加的代码删除了,代码有不少错误,改正后没有发现你说的问题,点击案钮,可以获得文件名,似乎不要使用UploadFile.value,直接使用strFile1 != "",总之要对控件有更好的控制还是使用Web自定义控件,参考
ms-help://MS.VSCC/MS.MSDNVS.2052/Vbcon/html/vbconwebusercontrolsvscustomwebcontrols.htm