C# 如何创建现有用户控件的自定义控件
我已经为C# 如何创建现有用户控件的自定义控件,c#,asp.net,custom-controls,C#,Asp.net,Custom Controls,我已经为多文件上载创建了一个用户控件, 我需要创建它的自定义控件,这样我就可以拥有该控件的dll。 我有什么方法可以做到这一点 usercontrol.ascx <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script src="Scripts/jquery.MultiFile.pack.js" type="text/javascript"><
多文件上载创建了一个用户控件
,
我需要创建它的自定义控件,这样我就可以拥有该控件的dll。
我有什么方法可以做到这一点
usercontrol.ascx
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.MultiFile.pack.js" type="text/javascript"></script>
<div><%-- accept attribute can be used like accept="png|jpg"--%>
Multiple File Upload<br />
<asp:FileUpload ID="FileUpload10" runat="server" class="multi" accept="" />
<asp:Button ID="Button3" runat="server" Text="Submit" OnClick="jQueryUploadFiles" />
<br />
<asp:Label ID="lblMessage" runat="server" EnableViewState="false" ForeColor="Green" />
<br />
<asp:Label ID="lblError" runat="server" EnableViewState="false" ForeColor="Red" />
</div>
多文件上传
usercontrol.ascx.cs
private void FileUploadUsingJQuerySelectionMethod()
{
// check if file has been selected
HttpFileCollection files = Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
if (file.ContentLength > 0)
{
string path = ConfigurationManager.AppSettings["FilePath"];
string fileName = Path.GetFileName(file.FileName);
// now save the file to the disk
file.SaveAs(path + fileName);
lblMessage.Text += "File : <b>" + fileName + "</b> uploaded successfully !<br />";
}
}
}
使用jQuerySelectionMethod()上载私有void文件
{
//检查是否已选择文件
HttpFileCollection files=Request.files;
对于(int i=0;i”; } } } 我试着如下:
public class MultipleFileUpload : WebControl
{
#region declare controls here
Label lblMessage;
Label lblError;
FileUpload FileUpload10;
Button btnUpload;
#endregion
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string FilePath
{// prop to get filepath
get
{
String s = (String)ViewState["FilePath"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["FilePath"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(FilePath);
}
// create the layout (html) of your control here
// all the HTML code including <div>
// Add all controls to the <div>, below code is very crude.<br/>
// Also you need to register the script tags and add the script to it<br/>
protected override void CreateChildControls()
{
base.CreateChildControls();
Table table = new Table();
this.Controls.Add(table);
lblMessage = new Label();
lblMessage.ID = "lblMessage";
lblError = new Label();
lblError.ID = "lblError";
FileUpload10 = new FileUpload();
FileUpload10.ID = "FileUpload10";
btnUpload = new Button();
btnUpload.ID = "btnUpload";
btnUpload.Text = "Submit <br/> ";
// table.Controls.Add(lblMessage);
}
// invoke this method were ever required
private void FileUploadUsingJQuerySelectionMethod()
{
// check if file has been selected
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
if (file.ContentLength > 0)
{
string path = FilePath;
string fileName = Path.GetFileName(file.FileName);
// now save the file to the disk
file.SaveAs(path + fileName);
lblMessage.Text += "File : <b>" + fileName + "</b> uploaded successfully !<br />";
}
}
}
公共类多功能加载:网络控制
{
#区域在此声明控件
标签信息;
标签lblError;
文件上传文件上传10;
按钮btnUpload;
#端区
[可装订(真实)]
[类别(“外观”)]
[默认值(“”)
[可本地化(正确)]
公共字符串文件路径
{//prop以获取文件路径
得到
{
字符串s=(字符串)视图状态[“文件路径”];
返回((s==null)“[”+this.ID+“]”:s);
}
设置
{
ViewState[“文件路径”]=值;
}
}
受保护的覆盖无效渲染内容(HtmlTextWriter输出)
{
output.Write(文件路径);
}
//在此处创建控件的布局(html)
//所有的HTML代码包括
//将所有控件添加到,下面的代码非常粗糙。
//您还需要注册脚本标记并将脚本添加到其中
受保护的覆盖无效CreateChildControls()
{
base.CreateChildControls();
Table Table=新表();
this.Controls.Add(表);
LBL消息=新标签();
lblMessage.ID=“lblMessage”;
lblError=新标签();
lblError.ID=“lblError”;
FileUpload10=新建FileUpload();
FileUpload10.ID=“FileUpload10”;
btnUpload=新建按钮();
btnUpload.ID=“btnUpload”;
btnUpload.Text=“提交
”;
//表.控件.添加(LBL消息);
}
//调用此方法是必需的
使用jQuerySelectionMethod()上载私有void文件
{
//检查是否已选择文件
HttpFileCollection files=HttpContext.Current.Request.files;
对于(int i=0;i0)
{
字符串路径=文件路径;
字符串文件名=Path.GetFileName(file.fileName);
//现在将文件保存到磁盘
file.SaveAs(路径+文件名);
lblMessage.Text+=“文件:“+fileName+”已成功上载!
”;
}
}
}
您可以按照此处详述的步骤将控件放入dll中:
我认为将您的用户控件转换为适当的服务器控件是值得的,但是,这并不难,最终您将得到更易于维护的代码(正如您将看到的,这里描述的过程相当尴尬).若要生成web控件,您需要从UserControl或其他控件(在您的情况下为FileUpload)继承,然后重写init事件以将其他控件(例如按钮)添加到树中。根据需要重写任何其他事件 旧文章,但相当清楚,例如,委托人:
您可以通过将js文件嵌入dll来添加js文件,即
只需通过覆盖渲染将链接添加到它们