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

时间:2022-06-01 19:38:20

I am trying to implement a custom control using a RowClickableGridView class provided on this Stack Overflow post. This is the first time I have tried to create a custom server control and followed steps laid out in this MSDN walkthrough.

我正在尝试使用此Stack Overflow帖子上提供的RowClickableGridView类来实现自定义控件。这是我第一次尝试创建自定义服务器控件并按照此MSDN演练中的步骤进行操作。

I have the RowClickableGridView class in the App\_Code directory of my Web Application Project with a namespace of MyWebApplication.App\_Code, and it compiles.

我在我的Web应用程序项目的App \ _Code目录中有一个RowClickableGridView类,其名称空间为MyWebApplication.App \ _Code,并进行编译。

My problem is that the .aspx page that I am trying to use the control on does not recognize the tag prefix. The page also has numerous warnings for unsupported elements between the cc1:GridViewRowClickable tags. I thought I had everything in place according to the MSDN walkthrough.

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

Code Snippet

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

Any idea on what I am doing wrong or suggestions on what to try next?

我对我做错了什么或者下一步尝试的建议有什么想法吗?

2 个解决方案

#1


4  

You have specified "RowClickableGridView" as the TagName but you are using "GridViewRowClickable" in the code.

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

#2


1  

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

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

  1. Create a new ASP.NET Server Control Project
  2. 创建一个新的ASP.NET服务器控件项目
  3. Copy class into default cs file and renamed namespace.
  4. 将类复制到默认cs文件并重命名命名空间。
  5. Add default TagPrefix to line above namespace declaration.
    [assembly: TagPrefix("mynamespace", "mycustomtag")]
  6. 将默认TagPrefix添加到命名空间声明上方的行。 [assembly:TagPrefix(“mynamespace”,“mycustomtag”)]
  7. Add ToolBoxData to line above class copied.
    [ToolboxData("<{0}:GridViewRowClickable runat=server></{0}:GridViewRowClickable>")]
  8. 将ToolBoxData添加到复制的类上面的行。 [ToolboxData(“<{0}:GridViewRowClickable runat = server> ”)]
  9. Build project into dll
  10. 将项目构建到dll中
  11. Copy dll to bin directory of Web Application
  12. 将dll复制到Web应用程序的bin目录
  13. Reference dll in Web Application Project
  14. Web应用程序项目中的引用dll
  15. Add controls to toolbox by adding creating a new toolbox item from the dll
  16. 通过添加从dll创建新工具箱项来向工具箱添加控件
  17. Drag and drop control from toolbox into aspx page
  18. 将控件从工具箱拖放到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.

这会在aspx页面的顶部添加适当的Register指令并修复我收到的所有警告。自动完成也适用于这种情况。

Below is the 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>

#1


4  

You have specified "RowClickableGridView" as the TagName but you are using "GridViewRowClickable" in the code.

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

#2


1  

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

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

  1. Create a new ASP.NET Server Control Project
  2. 创建一个新的ASP.NET服务器控件项目
  3. Copy class into default cs file and renamed namespace.
  4. 将类复制到默认cs文件并重命名命名空间。
  5. Add default TagPrefix to line above namespace declaration.
    [assembly: TagPrefix("mynamespace", "mycustomtag")]
  6. 将默认TagPrefix添加到命名空间声明上方的行。 [assembly:TagPrefix(“mynamespace”,“mycustomtag”)]
  7. Add ToolBoxData to line above class copied.
    [ToolboxData("<{0}:GridViewRowClickable runat=server></{0}:GridViewRowClickable>")]
  8. 将ToolBoxData添加到复制的类上面的行。 [ToolboxData(“<{0}:GridViewRowClickable runat = server> ”)]
  9. Build project into dll
  10. 将项目构建到dll中
  11. Copy dll to bin directory of Web Application
  12. 将dll复制到Web应用程序的bin目录
  13. Reference dll in Web Application Project
  14. Web应用程序项目中的引用dll
  15. Add controls to toolbox by adding creating a new toolbox item from the dll
  16. 通过添加从dll创建新工具箱项来向工具箱添加控件
  17. Drag and drop control from toolbox into aspx page
  18. 将控件从工具箱拖放到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.

这会在aspx页面的顶部添加适当的Register指令并修复我收到的所有警告。自动完成也适用于这种情况。

Below is the 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>