C# 如何实现C自定义服务器控件?
我正在尝试使用上提供的RowClickableGridView类实现自定义控件。这是我第一次尝试创建自定义服务器控件,并遵循中列出的步骤 我的Web应用程序项目的App\\代码目录中有RowClickableGridView类,名称空间为MyWebApplication.App\\代码,它可以编译 我的问题是,我试图在其上使用控件的.aspx页面无法识别标记前缀。对于cc1:GridViewRowClickable标记之间不支持的元素,该页面也有许多警告。我以为我已经根据MSDN演练安排好了一切 代码片段C# 如何实现C自定义服务器控件?,c#,.net,asp.net,user-controls,ascx,C#,.net,Asp.net,User Controls,Ascx,我正在尝试使用上提供的RowClickableGridView类实现自定义控件。这是我第一次尝试创建自定义服务器控件,并遵循中列出的步骤 我的Web应用程序项目的App\\代码目录中有RowClickableGridView类,名称空间为MyWebApplication.App\\代码,它可以编译 我的问题是,我试图在其上使用控件的.aspx页面无法识别标记前缀。对于cc1:GridViewRowClickable标记之间不支持的元素,该页面也有许多警告。我以为我已经根据MSDN演练安排好了一切
你知道我做错了什么,或者对下一步尝试什么有什么建议吗?你指定了RowClickableGridView作为标记名,但你在代码中使用了GridViewRowClickable。你指定了RowClickableGridView作为标记名,但你在代码中使用了GridViewRowClickable。我终于让它工作了。不过我采取了不同的方法
<%@ 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>
创建新的ASP.NET服务器控件项目
将类复制到默认cs文件并重命名命名空间。
将默认标记前缀添加到命名空间声明上方的行。[程序集:TagPrefixmynamespace,mycustomtag]
将ToolBoxData添加到复制的类上方的行中。[工具箱数据]
将项目构建到dll中
将dll复制到Web应用程序的bin目录
Web应用程序项目中的引用dll
通过添加从dll创建的新工具箱项,将控件添加到工具箱
将控件从工具箱拖放到aspx页面
这在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>
我终于成功了。不过我采取了不同的方法 创建新的ASP.NET服务器控件项目 将类复制到默认cs文件并重命名命名空间。 将默认标记前缀添加到命名空间声明上方的行。[程序集:TagPrefixmynamespace,mycustomtag] 将ToolBoxData添加到复制的类上方的行中。[工具箱数据] 将项目构建到dll中 将dll复制到Web应用程序的bin目录 Web应用程序项目中的引用dll 通过添加从dll创建的新工具箱项,将控件添加到工具箱 将控件从工具箱拖放到aspx页面 这在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>
+1用于指出有问题的错误键入。不幸的是,我使用的标记名和标记名是相同的名称,因此这并不能修复我的错误。我刚刚将类名更改为GridViewRowClickable,因为我喜欢在名称中首先使用GridView。我已经编辑了这个问题,以显示正确的名称。+1用于指出问题中的错误键入。不幸的是,我使用的标记名和标记名是相同的名称,因此这并不能修复我的错误。我刚刚将类名更改为GridViewRowClickable,因为我喜欢在名称中首先使用GridView。我已对问题进行了编辑,以显示所用的正确名称。