且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何实现C#的自定义服务器控件?

更新时间:2023-10-14 14:27:58

我得到它的工作最后。我采取了不同的方法,但。

I got it to work finally. I took a different approach though.

  1. 创建一个新的ASP.NET服务器控件项目
  2. 复制类违约CS文件并重新命名的命名空间。
  3. 添加默认标签preFIX行上述命名空间声明
    [总成:标签preFIX(myNameSpace对象,mycustomtag)]
  4. 添加ToolBoxData行上面的类复制。
    [ToolboxData(< {0}:GridViewRowClickable RUNAT =服务器>< / {0}:GridViewRowClickable>中)]
  5. 在建项目到DLL
  6. 复制DLL到Web应用程序的bin目录
  7. 引用的DLL中的Web应用程序项目
  8. 添加控件工具箱中添加从DLL创建一个新的工具箱项
  9. 将和从工具箱中拖放控制到aspx页面
  1. Create a new ASP.NET Server Control Project
  2. Copy class into default cs file and renamed namespace.
  3. Add default TagPrefix to line above namespace declaration.
    [assembly: TagPrefix("mynamespace", "mycustomtag")]
  4. Add ToolBoxData to line above class copied.
    [ToolboxData("<{0}:GridViewRowClickable runat=server></{0}:GridViewRowClickable>")]
  5. Build project into dll
  6. Copy dll to bin directory of Web Application
  7. Reference dll in Web Application Project
  8. Add controls to toolbox by adding creating a new toolbox item from the dll
  9. Drag and drop control from toolbox into aspx page

这增加了相应的Register指令在aspx页面的顶部,并修复了所有我收到的警告。自动完成也可以在此情况下,也

This adds the appropriate Register directive at the top of the aspx page and fixed all the warnings I received. The auto complete also works in this case as well.

下面是code。

<%@ Page Title="" Language="C#" MasterPageFile="~/MyMaster.master" AutoEventWireup="true" Inherits="MyPage" Codebehind="MyPage.aspx.cs" %>
<%@ Register Assembly="GridViewRowClickable" Namespace="CustomServerControls" TagPrefix="MyTag" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:SqlDataSource ID="Sql_MyTable" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
        SelectCommand="spTbl_Select" SelectCommandType="StoredProcedure">
    </asp:SqlDataSource>
    <egcc:GridViewRowClickable ID="GridViewRowClickable_test" runat="server" 
        DataSourceID="Sql_MyTable" DataKeyNames="tbl_id"
        AllowSorting="True" AutoGenerateColumns="False" GridLines="None" PageSize="25" Width="100%"
        EnableRowClickSelection="true" RowClickCommand="Select" OnSelectedIndexChanged="GridViewRowClickable_test_OnSelectedIndexChanged">
        <Columns>
            <asp:BoundField HeaderText="ID" DataField="tbl_id" SortExpression="tbl_id" />
            <asp:BoundField HeaderText="Name" DataField="tbl_name" SortExpression="tbl_name" />
        </Columns>
        <EmptyDataTemplate>
            No Data.
        </EmptyDataTemplate>
    </egcc:GridViewRowClickable>
</asp:Content>