开发者

How to implement a C# custom server control?

I am trying to implement a custom control using a RowClickableGridView class provided on this Stack Overflow post. This is the first time I h开发者_JAVA技巧ave tried to create a custom server control and followed steps laid out in this MSDN walkthrough.

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.

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.

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?


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


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

  1. Create a new ASP.NET Server Control Project
  2. Copy class into default cs file and renamed namespace.
  3. Add default TagPrefix to line above namespace declaration.
    [assembly: TagPrefix("mynamespace", "mycustomtag")]
  4. Add ToolBoxData to line above class copied.
    [ToolboxData("<{0}:GridViewRowClickable runat=server></{0}:GridViewRowClickable>")]
  5. Build project into dll
  6. Copy dll to bin directory of Web Application
  7. Reference dll in Web Application Project
  8. Add controls to toolbox by adding creating a new toolbox item from the dll
  9. Drag and drop control from toolbox into aspx page

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.

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜