代码之家  ›  专栏  ›  技术社区  ›  Eddie

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

  •  1
  • Eddie  · 技术社区  · 16 年前

    我正在尝试使用 RowClickableGridView 上提供的课程 this Stack Overflow post . 这是我第一次尝试创建自定义服务器控件,并遵循中列出的步骤 this MSDN walkthrough

    我有 行可单击网格视图 课堂 App\_Code 命名空间为的Web应用程序项目的目录 MyWebApplication.App\_Code

    我的问题是 .aspx 我尝试在其上使用控件的页面无法识别标记前缀。该页面还包含多个警告,用于警告服务器之间不支持的元素 cc1:GridViewRowClickable

    代码片段

    <%@ Page Title="MyPage" Language="C#" MasterPageFile="~/MyMaster.master" AutoEventWireup="true" Inherits="MyPage" Codebehind="MyPage.aspx.cs" %>
    <%@ Register TagPrefix="cc1" TagName="RowClickableGridView" Namespace="MyWebApplication.App_Code" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" SelectCommand="MySpName" SelectCommandType="StoredProcedure">
        </asp:SqlDataSource>
        <cc1:RowClickableGridView ID="GVW_test" runat="server" DataSourceID="SqlDataSource1">
            <HeaderStyle CssClass="ListTop" />
            <RowStyle CssClass="RowHighlight" />
            <Columns>
                <asp:BoundField HeaderText="ID" DataField="Atr_ID" SortExpression="Atr_ID" />
                <asp:BoundField HeaderText="Name" DataField="Atr_Name" SortExpression="Atr_Name" />
            </Columns>
            <EmptyDataTemplate>
                No Data
            </EmptyDataTemplate>
       </cc1:RowClickableGridView>
    </asp:Content>
    

    你知道我做错了什么,或者对下一步该怎么做有什么建议吗?

    2 回复  |  直到 6 年前
        1
  •  4
  •   Tim Ebenezer    16 年前

    您已将“RowClickableGridView”指定为标记名,但在代码中使用的是“GridViewRowClickable”。

        2
  •  1
  •   Eddie    16 年前

    我终于成功了。不过我采取了不同的方法。

    1. 将类复制到默认cs文件并重命名命名空间。

    2. [assembly: TagPrefix("mynamespace", "mycustomtag")]
    3. 将ToolBoxData添加到复制的类上方的行中。
      [ToolboxData("<{0}:GridViewRowClickable runat=server></{0}:GridViewRowClickable>")]
    4. 将项目构建到dll中
    5. Web应用程序项目中的引用dll
    6. 通过添加从dll创建的新工具箱项,将控件添加到工具箱
    7. 将控件从工具箱拖放到aspx页面

    <%@ 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>