1

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

我在我的 Web 应用程序项目RowClickableGridView的目录中有一个命名空间为 的类,它可以编译。App\_CodeMyWebApplication.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>

关于我做错了什么的任何想法或关于下一步尝试的建议?

4

2 回答 2

4

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

于 2009-12-03T14:40:39.383 回答
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. 将控件从工具箱拖放到 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>
于 2009-12-04T17:58:51.103 回答