如何创建现有用户控件的自定义控件

编程入门 行业动态 更新时间:2024-10-26 17:23:53
本文介绍了如何创建现有用户控件的自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经创建了一个用户控制多文件上传,我需要创建其自定义的控制,这样我可以有控制的DLL。什么是我能做到这一点的方法是什么?

usercontrol.ascx

<脚本SRC =脚本/ jQuery的-1.4.1.min.js类型=文/ JavaScript的>< / SCRIPT>  <脚本SRC =脚本/ jquery.MultiFile.pack.js类型=文/ JavaScript的>< / SCRIPT>    < D​​IV><% - 接受属性可用于像接受=PNG | JPG - %GT;                    多文件上传< BR />                    < ASP:文件上传ID =FileUpload10=服务器类=多接受=/>                    < ASP:按钮的ID =按钮3=服务器文本=提交的OnClick =jQueryUploadFiles/>        < BR />        < ASP:标签ID =lblMessage=服务器的EnableViewState =假前景色=绿色/>        < BR />        < ASP:标签ID =lblError=服务器的EnableViewState =假前景色=红/>    < / DIV>

usercontrol.ascx.cs

私人无效FileUploadUsingJQuerySelectionMethod()        {            //如果文件被选中            HttpFileCollection文件= Request.Files;            的for(int i = 0; I< files.Count;我++)            {                HttpPostedFile文件=文件[I]                如果(file.ContentLength大于0)                {                    字符串路径= ConfigurationManager.AppSettings [文件路径];                    字符串文件名= Path.GetFileName(file.FileName);                    //现在将文件保存到磁盘                    file.SaveAs(路径+文件名);                    lblMessage.Text + =文件:其中; B>中+文件名+< / B>!成功上传< BR />中;                }            }        }

我试着像下面这样:

公共类MultipleFileUpload:WebControl的{   #地区在此申报控制    标签lblMessage;    标签lblError;    文件上传FileUpload10;    按钮btnUpload;   #endregion    [可绑定(真)    [类别(外观)]    [默认值()]    [本地化(真)    公共字符串的文件路径    {//道具来获得文件路径        得到        {            字符串s =(字符串)的ViewState [文件路径];            返回((S == NULL)[+ this.ID +]:秒);        }        组        {            的ViewState [文件路径] =值;        }    }    保护覆盖无效RenderContents(HtmlTextWriter的输出)    {        output.Write(文件路径);    }   //这里创建您的控件布局(HTML)   //所有的HTML code&包括LT; D​​IV>   //添加所有控件的< DIV>中低于code是非常粗略< BR />   //你也需要注册脚本标记和脚本添加到它< BR />    保护覆盖无效的CreateChildControls()    {        base.CreateChildControls();        表的表=新表();        this.Controls.Add(表);        lblMessage =新标签();        lblMessage.ID =lblMessage;        lblError =新标签();        lblError.ID =lblError;        FileUpload10 =新的FileUpload();        FileUpload10.ID =FileUpload10;        btnUpload =新按钮();        btnUpload.ID =btnUpload;        btnUpload.Text =提交< BR />中;       // table.Controls.Add(lblMessage);    }    //调用该方法被不断要求    私人无效FileUploadUsingJQuerySelectionMethod()    {        //如果文件被选中        HttpFileCollection文件= HttpContext.Current.Request.Files;        的for(int i = 0; I< files.Count;我++)        {            HttpPostedFile文件=文件[I]            如果(file.ContentLength大于0)            {                路径字符串=文件路径;                字符串文件名= Path.GetFileName(file.FileName);                //现在将文件保存到磁盘                file.SaveAs(路径+文件名);                lblMessage.Text + =文件:其中; B>中+文件名+< / B>!成功上传< BR />中;            }        }    }

解决方案

您可以把你的控制下在这里详细步骤一个dll:的打开一个的.ascx用户控件成可再发行自定义控件。

我认为这将是值得你的用户控件转换到正确的服务器控制,但是,它并不难,你最终会得到更易于维护code(你会看到,这个过程描述的有颇为尴尬的)。

I have created one user control for multiple file upload , i need to create its custom control so that I can have a dll of that control. What are the ways that I can do this?

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 />"; } } }

I tried like following:

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 />"; } } }

解决方案

You can put your control in a dll following the steps detailed here: Turning an .ascx User Control into a Redistributable Custom Control.

I think that it would be worth converting your user control to a proper server control, however, it's not that hard and you would end up with easier to maintain code (as you will see, the process described there is rather awkward).

更多推荐

如何创建现有用户控件的自定义控件

本文发布于:2023-11-11 17:51:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579146.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   自定义   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!