[英]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”。
我終於明白了。 不過,我采取了另一種方法。
[assembly: TagPrefix("mynamespace", "mycustomtag")]
[ToolboxData("<{0}:GridViewRowClickable runat=server></{0}:GridViewRowClickable>")]
這將在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.