none
用户自定义控件如何封装成可添加到工具箱直接就能拖拽使用的dll文件? RRS feed

  • 问题

  • 自己写了个上传图片的用户自定义控件,想封装成dll,添加到工具箱,可以直接拖拽使用,使用时设置相关属性即可(类似分页控件的那种),没有相关的知识,不知如何封装?请个位高手给个教程,或者思路(要学习哪些相关知识)?如果自定义控件还调用了js或jQuery,可以一起封装到dll里吗?代码如下

     

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ImgUpLoad.ascx.cs" Inherits="Ctrls_ImgUpLoad" %>
    <div class="UC_UpImgContainer">
        <asp:Image ID="Img" CssClass="UC_UpImg" runat="Server" BorderStyle="Solid" BorderWidth="1"
            BorderColor="#D0D0D0" Width="125px" Height="90px" onerror="javascript:this.src='../Images/nophoto.jpg'" /><br />
        <asp:Label ID="lbTitle" runat="Server">图片地址:</asp:Label>
        <asp:TextBox ID="TbImgUrl" runat="Server"></asp:TextBox>
        <asp:Button ID="BtnUrl" CausesValidation="false" runat="Server" Text=" 确 定 " OnClick="BtnUrl_Click" /><br />
        上传图片:<asp:FileUpload ID="FuImg" runat="Server" /><br />
        <asp:Button ID="UpBtn" runat="Server" CausesValidation="false" Text="上传图片" OnClick="UpBtn_Click" />
        <asp:HiddenField ID="ImgUrl" Value="" runat="Server" />
        <asp:HiddenField ID="HFArg" Value="" runat="Server" />
    
        <script type="text/javascript">
            $("#<%=this.BtnUrl.ClientID %>").click(function() {
                var TbImgUrl = $("#<%=this.TbImgUrl.ClientID %>");
                if (!TbImgUrl.val()) {
                    alert("请输入图处地址");
                    return false;
                }
            });
        </script>
    
    </div>
    

    public partial class Ctrls_ImgUpLoad : System.Web.UI.UserControl
    {
        public int ImgSize { get; set; }
        public int ImgHeight { get; set; }
        public int ImgWidth { get; set; }
        public bool DisEnable { get; set; }
    
        /// <summary>
        /// 图片地址
        /// </summary>
        public string ImgSrc { get { return ImgUrl.Value; } set { Img.ImageUrl = value; ImgUrl.Value = value; TbImgUrl.Text = value; } }
    
        public string CommandArg { get { return HFArg.Value; } set { HFArg.Value = value; } }
    
        public string CommandName { get; set; }
        public event EventHandler ImgChanged;
        ClientScriptManager csm;
    
        protected void Page_Init(object sender, EventArgs e)
        {
           
            csm = this.Page.ClientScript;
            UpBtn.Attributes.Add("OnClick", "return CheckNoneImg('#" + this.FuImg.ClientID + "');");
            if (ImgWidth > 0) { Img.Width = ImgWidth; }
            if (ImgHeight > 0) { Img.Height = ImgHeight; }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void BtnUrl_Click(object sender, EventArgs e)
        {
            Img.ImageUrl = TbImgUrl.Text;
            ImgUrl.Value = TbImgUrl.Text;
            if (ImgChanged != null)
            {
                ImgChanged.Invoke(this, null);
            }
        }
    
        void admin_Ctrls_ImgUpLoad_ImgChanged(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    
        protected void UpBtn_Click(object sender, EventArgs e)
        {
            if (!FuImg.HasFile)
            {
                WebUtility.MsgBox("请选择图片!"); 
                return;
            }
            string fileName = string.Empty;
            if (ImgSize <= 0) ImgSize = 500;
            WebUtility.SaveImage(FuImg, ImgSize, out fileName);
            if (string.IsNullOrEmpty(fileName))
            {
                WebUtility.MsgBox("请选择图片!"); 
                return;
            }
            TbImgUrl.Text = fileName;
            ImgUrl.Value = fileName;
            Img.ImageUrl = fileName;
            if (ImgChanged != null)
            {
                ImgChanged.Invoke(this, null);
            }
        }
    }
    
    



    2011年12月1日 3:34

答案

全部回复