无法在我的aspx页面中调用用户控件 [英] Can not call the user control in my aspx page

查看:73
本文介绍了无法在我的aspx页面中调用用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加一个新的WebForm(带有母版页)并在其上拖动用户控件。当我构建解决方案时,它会显示此错误 - 成员名称不能与其封闭类型相同



我尝试了什么:



 <%@     Page    标题  =    语言  =  C#    MasterPageFile   = 〜/ Main.master    AutoEventWireup   =  true    CodeBehind   =  BuyerInspection.aspx.cs   继承  =  WebApp.Pages.BuyerInspection   %>  
<%@ 注册 Src = 〜/ Controls / BuyerInspection.ascx TagPrefix = uc TagName = BuyerInspection %>
< asp:内容 ID = Content1 ContentPlaceHolderID = PageTitleContentPlaceHolder runat = 服务器 > 银行付款
< asp:内容 ID = Content2 ContentPlaceHolderID = PageContentPlaceHolder runat = 服务器 >
< div data-flow = row >
< uc:BuyerInspection ID = BuyerInsp runat < span class =code-keyword> = server > < / div >

解决方案

您的控件的ID是BuyerInspection,控件的类型也是BuyerInspection,就像页面的类一样,所以在幕后(在你的设计师档案)你有这样的东西;



<前lang =C#> 公共 < span class =code-keyword> partial class BuyerInspection
{
protected BuyerInspection BuyerInspection;
}





在.net中,你不能拥有与班级名称相同的属性名称,所以你可以通过调用你的控件来解决这个问题



< uc:BuyerInspection ID =ucBuyerInspectionrunat =server> 





将代码更改为



  public   partial   class  BuyerInspection 
{
protected BuyerInspection ucBuyerInspection;
}





一般来说,你需要采用更好的命名约定,我会给控件的类型一个反映它的名字是一个控件,以区别于同名页面。


Add a new WebForm(with master page) and drag the user control on it.When I build the solution it shows this error-"member names cannot be the same as their enclosing type"

What I have tried:

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeBehind="BuyerInspection.aspx.cs" Inherits="WebApp.Pages.BuyerInspection" %>
<%@ Register Src="~/Controls/BuyerInspection.ascx" TagPrefix="uc" TagName="BuyerInspection" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PageTitleContentPlaceHolder" runat="Server">Bank Payment
<asp:Content ID="Content2" ContentPlaceHolderID="PageContentPlaceHolder" runat="Server">
    <div data-flow="row">
        <uc:BuyerInspection ID="BuyerInspection"  runat="server"></div>

解决方案

The ID of your control is BuyerInspection and the type of the control is also BuyerInspection, as is the class of your page, so behind the scenes (in your designer file) you have something like this;

public partial class BuyerInspection
{
    protected BuyerInspection BuyerInspection;
}



In .net you can't have a property name that is the same as the class name, so you can fix this by calling your control something else

<uc:BuyerInspection ID="ucBuyerInspection"  runat="server">



which will change the code to

public partial class BuyerInspection
{
    protected BuyerInspection ucBuyerInspection;
}



In general you need to employ better naming conventions anyway, I would give the control's type a name that reflects it is a control to distinguish it from the page of the same name.


这篇关于无法在我的aspx页面中调用用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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