如何以DLL文件形式创建和使用用户控件

8 篇文章 0 订阅
6 篇文章 0 订阅

原文地址链接:http://www.codeproject.com/Articles/415109/How-to-Create-and-Use-a-User-Control-as-a-DLL

PS:用户控件,不是自定义控件。DLL文件形式,不是ASCX文件形式。

Introduction(说明)

In this section, you will learn how to build a custom control as a DLL. The basic feature of the DLL is we can use it as a usercontol like a .ascx.

在这一章中,你将会学到如何以DLL文件形式创建一个用户控件。该DLL文件的基本特性是,我们可以像ascx文件那样将它用作用户控件。

How to add a JavaScript file in a DLL(如何添加一个JS文件到DLL中)

First create a new project and select Class Library project.(首先创建一个新项目,选择类库类型)

Figure 1. Add class Library Project(图1,,添加类图项目)
  1. 图1

Add a JavaScript file and in the Properties window, set properties as in figure 2.(添加一个JavaScript文件,在其属性窗口中,如图2所示设置属性。)

图2

Now in the AssemblyInfo.cs file, add the following lines. These are very important lines so add exactly the same name of the js file.

然后在AssemblyInfo.cs文件中添加如下几行,这些行非常重要,因此确保JS文件名书写正确。

[assembly: System.Web.UI.WebResource("AddUserControl.JSAutoCmp.js", "text/javascript", PerformSubstitution = true)]

Now we write code for creating an autocomplete textbox with a listbox control.

接着写代码实现一个自动完成功能,随着文本框TextBox组件输入的内容的变换,列表框ListBox的选中值随之更改。

We will create an autotexbox with a listbox. When we type in the textbox our focus automatically will go to the listbox matched item. We write [ToolboxData(@"<{0}:AutoCmp_TextBox runat=""server"" \>")]. By ToolboxData, we specify the tag which is added to the page when this control is dragged form the toolbox to a form. We use AutoCompleteType which enables us to associate an AutoComplete class with the TextBox control.

我们要创建的用户控件是一个文本框控制的自动完成列表框。当我们在文本框中输入内容时,列表框中批评项会自动获取焦点。

在类前加入属性内容:

[ToolboxData(@"<{0}:AutoCmp_TextBox runat=""server"" \>")]

PS:该属性内容用于工具箱操作上。

通过工具箱数据ToolBoxData,我们指定了当该控件被从工具箱拖放到窗体后,页面文件中要添加的标签。

PS:类库项目中,添加文件选择普通类文件,然后引入和Web相关的命名空间即可。

[ToolboxData(@"<{0}:AutoCmp_TextBox runat=""server"" \>")]
public class AutoComplete_TextBox : TextBox
{
    private string _AutoComplete_ListBoxId = "";
    private string _Selected_Value = "-1";

    public AutoComplete_TextBox()
        : base()
    {
        this.AutoCompleteType = AutoCompleteType.Disabled;
    }

    [Category("Behavior"), DefaultValue(""),
      Description("The AutoComplete_ListBox's Id which will apear on key press.")]
    public string AutoComplete_ListBoxId
    {
        get { return _AutoComplete_ListBoxId; }
        set { _AutoComplete_ListBoxId = value.Trim(); }
    }

    [Category("Behavior"), DefaultValue(""),
      Description("Selected value in assossiated AutoComplete_ListBoxId.")]
    public string Selected_Value
    {
        get
        {
            try
            {
                if (HttpContext.Current.Request.Form.Count > 0 && _AutoComplete_ListBoxId.Trim() != "")
                    if (HttpContext.Current.Request.Form["SanControls_HdnField"] != null)
                        _Selected_Value = get_SelectedValue(HttpContext.Current.Request.Form["SanControls_HdnField"]);
                    return _Selected_Value;
                }
            catch { return "-1"; }
        }
        set { _Selected_Value = value; }
    }

    private void setCtrl_Attribute(string Attr_name, string value)
    {
        if (Attr_name != null && Attr_name != "" && value != null && value != "")
        {
            if (this.Attributes[Attr_name] != null && this.Attributes[Attr_name] != "")
            {
                if (this.Attributes[Attr_name].IndexOf(value, StringComparison.OrdinalIgnoreCase) == -1)
                    this.Attributes[Attr_name] = value + ";" + this.Attributes[Attr_name];
            }
            else
                this.Attributes[Attr_name] = value + ";";
        }
    }

    private string get_SelectedValue(string viewState)
    {
        string retVal = _Selected_Value;
        string[] values ={ "|;" };
        if (viewState != null)
        {
            values = viewState.Split(values, StringSplitOptions.None);
            for (int i = 0; i < values.Length; i++)
            {
                if (values[i].Trim() != "" && values[i].Substring(0, values[i].IndexOf("=")) == this.ClientID)
                {
                    retVal = values[i].Substring(values[i].IndexOf("=") + 1).Trim();
                    break;
                }
            }
        }
        return retVal;
    }


    protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
    {
        writer.AddAttribute("SelectedIndex", "-1");
        writer.AddAttribute("SelectedValue", Selected_Value);
        base.AddAttributesToRender(writer);
    }

    protected override void OnPreRender(EventArgs e)
    {
        if (AutoComplete_ListBoxId != "")
        {
            setCtrl_Attribute("onkeyup", "SanControls_selectItem()");
            //"return SanControls_showList('" + 
            //  Page.FindControl(AutoCompleteListId).ClientID + "')");
            setCtrl_Attribute("onblur", "SanControls_hideList()");
            setCtrl_Attribute("onkeydown", "checkKey('" + 
               Page.FindControl(AutoComplete_ListBoxId).ClientID + "')");
            setCtrl_Attribute("onfocus", "SanControls_TextFocus('" + 
               Page.FindControl(AutoComplete_ListBoxId).ClientID + "')");
        }

        Page.RegisterRequiresPostBack(this);
        Page.ClientScript.RegisterClientScriptInclude("SanControlsJS", 
             Page.ClientScript.GetWebResourceUrl(this.GetType(), "AddUserControl.JSAutoCmp.js"));
        base.OnPreRender(e);
    }
}


AutoComplete_ListBoxId: By this we assign the listbox ID to the textbox in which we find the matched value and set focus on this item of the listbox. We call the Page.RegisterRequiresPostBack() method inside (or before) the control's PreRender() event to tell page that our control is interested in receiving postback data. All the client-side functionalities happen in an external JavaScript file. A JavaScript include is rendered by the control in its OnPreRender() method. This line is included in the JavaScript file.

AutoComplete_ListBoxId字段说明:通过为该字段赋值,我们在文本框内容改变时,设置要更改的列表框的项。

Page.ClientScript.RegisterClientScriptInclude("SanControlsJS", 
  Page.ClientScript.GetWebResourceUrl(this.GetType(), "AddUserControl.JSAutoCmp.js"));


Now we will create a listbox in which we find the value of the textbox.

现在我们要创建一个列表框ListBox,在列表框中有TextBox输入的内容的匹配项。

[ToolboxData(@"<{0}:AutoCmp_ListBox runat=""server"" \>")]
public class AutoCmp_ListBox : ListBox
{
    bool isrendered = false;
    public AutoCmp_ListBox()
        : base()
    {
        this.Rows = 7;

    }
    protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
    {
        isrendered = true;
        writer.AddAttribute("onMouseDown", "SanControls_SelectList();");
        writer.AddAttribute("onDblClick", "SanControls_hideList(true);");
        writer.AddAttribute("onMouseOut", "SanControls_deSelectList();");
        writer.AddAttribute("onMouseUp", "SanControls_deSelectList();");
        writer.AddAttribute("onChange", "SanControls_changeText();");
        base.AddAttributesToRender(writer);

    }

    protected override void OnPreRender(EventArgs e)
    {
        if (!isrendered)
        {
            if (this.Style.Value != null)
                this.Style.Value += "position:absolute;visibility:hidden;width:0px";
                else
                    this.Style.Value = "position:absolute;visibility:hidden;width:0px";

                Page.ClientScript.RegisterClientScriptInclude("SanControlsJS", 
                     Page.ClientScript.GetWebResourceUrl(this.GetType(), "AddUserControl.JSAutoCmp.js"));
                base.OnPreRender(e);

                if (HttpContext.Current.Request.Form["SanControls_HdnField"] == null)
                    Page.ClientScript.RegisterHiddenField("SanControls_HdnField", "");
                else
                    Page.ClientScript.RegisterHiddenField("SanControls_HdnField", 
                       HttpContext.Current.Request.Form["SanControls_HdnField"]);
            }
        }
    }

We build the DLL then add a New Website and add a reference of this DLL. In our aspx page, we register the assembly just as we use user controls.

编译该项目为一DLL文件,然后添加一个新的Website 网站(Web项目),并添加该DLL的引用。

在web项目的aspx文件中,像使用用户控件ascx一样添加assembly信息。

aspx page design

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly ="AddUserControl" Namespace="AddUserControl" TagPrefix ="SANCtrl"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <SANCtrl:AutoComplete_TextBox runat ="server" id="sanTextBox" AutoComplete_ListBoxId ="sanListBox" ></SANCtrl:AutoComplete_TextBox>
        <SANCtrl:AutoCmp_ListBox runat ="server" ID ="sanListBox" ></SANCtrl:AutoCmp_ListBox>
    </div>
    </form>
</body>

Now the .cs page code for setting the texbox's listbox ID for searching and filling the list box with data. When we view this page in browser and type in the texbox, the listbox appears and our focus goes on to the matched item of the list box. We do not require any code for this.

sanTextBox.AutoComplete_ListBoxId = sanListBox.UniqueID;
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("Name");
dt.Rows.Add("1.", "Aashish Bhatia");
dt.Rows.Add("2.", "Anoop Bhatia");
dt.Rows.Add("3.", "Diljit Singh");
dt.Rows.Add("4.", "Rajeev Sharma");
dt.Rows.Add("5.", "Sanjay Bhatia");
       
sanListBox.DataTextField = "Name";
sanListBox.DataValueField = "Id";

sanListBox.DataSource = dt;
sanListBox.DataBind();

See the figure below which shows the functionality of the autocomplete text box with listbox.

See this figure of the Solution Explorer in which I display the DLL project and a website page for testing if the js functions are assigned to the ASPX controls or not.

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值