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

时间:2009-12-03 14:37:06

标签: c# .net asp.net user-controls ascx

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

我的Web应用程序项目的RowClickableGridView目录中有App\_Code类,名称空间为MyWebApplication.App\_Code,并且它已编译。

我的问题是我尝试使用该控件的.aspx页面无法识别标记前缀。对于cc1:GridViewRowClickable标记之间不受支持的元素,该页面还有许多警告。根据MSDN演练,我以为我已经准备好了一切。

代码段

<%@ 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 个答案:

答案 0 :(得分:4)

您已将“RowClickableGridView”指定为TagName,但您在代码中使用“GridViewRowClickable”。

答案 1 :(得分:1)

我终于开始工作了。我采取了不同的方法。

  1. 创建新的ASP.NET服务器控件项目
  2. 将类复制到默认cs文件并重命名名称空间。
  3. 将默认TagPrefix添加到命名空间声明上方的行。
    [assembly: TagPrefix("mynamespace", "mycustomtag")]
  4. 将ToolBoxData添加到复制的类上面的行。
    [ToolboxData("<{0}:GridViewRowClickable runat=server></{0}:GridViewRowClickable>")]
  5. 将项目构建到dll
  6. 将dll复制到Web应用程序的bin目录
  7. Web应用程序项目中的引用dll
  8. 通过添加从dll
  9. 创建新工具箱项目,将控件添加到工具箱
  10. 将控件从工具箱拖放到aspx页面
  11. 这会在aspx页面的顶部添加适当的Register指令并修复我收到的所有警告。自动完成也适用于这种情况。

    以下是代码。

    <%@ 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>
    
相关问题