繁体   English   中英

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

[英]How to implement a C# custom server control?

我正在尝试使用此Stack Overflow帖子中提供的RowClickableGridView类实现自定义控件。 这是我第一次尝试创建自定义服务器控件,并遵循此MSDN演练中列出的步骤。

我的Web应用程序项目的App\\_Code目录中具有RowClickableGridView类,其名称空间为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>

对我做错了什么有任何想法或对下一步尝试有何建议?

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

我终于明白了。 不过,我采取了另一种方法。

  1. 创建一个新的ASP.NET Server Control项目
  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. 将控件从工具箱拖放到ASPX页面

这将在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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM