如何实现C#的自定义服务器控件? [英] How to implement a C# custom server control?

查看:125
本文介绍了如何实现C#的自定义服务器控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用上提供的 RowClickableGridView 类的自定义控件=htt​​p://stackoverflow.com/questions/686240/making-an -entire行可点击-IN-A-GridView控件>这个堆栈溢出帖子。这是我第一次尝试创建一个自定义服务器控件和遵循的这个MSDN演练

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.

我有 RowClickableGridView 类中的应用我的Web应用程序项目的\ _ code 目录中对 MyWebApplication.App \ _ code ,它编译,一个命名空间

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.

我的问题是,的.aspx ,我试图用控制上不能识别标签preFIX页面。该页面还为 CC1之间的不支持的元素多次警告:GridViewRowClickable 标签。我以为我根据MSDN演练了一切就绪。

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.

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

推荐答案

我得到它的工作最后。我采取了不同的方法,但。

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

  1. 创建一个新的ASP.NET服务器控件项目
  2. 复制类违约CS文件并重新命名的命名空间。
  3. 添加默认标签preFIX行上述命名空间声明
    [总成:标签preFIX(myNameSpace对象,mycustomtag)]
  4. 添加ToolBoxData行上面的类复制。
    [ToolboxData(&LT; {0}:GridViewRowClickable RUNAT =服务器&GT;&LT; / {0}:GridViewRowClickable&gt;中)]
  5. 在建项目到DLL
  6. 复制DLL到Web应用程序的bin目录
  7. 引用的DLL中的Web应用程序项目
  8. 添加控件工具箱中添加从DLL创建一个新的工具箱项
  9. 将和从工具箱中拖放控制到aspx页面
  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

这增加了相应的Register指令在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.

下面是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>

这篇关于如何实现C#的自定义服务器控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆