简体   繁体   English

我可以通过ASP.Net中的页面/控件的前端将类实例传递给自定义用户控件吗?

[英]Can I pass a class instance to a custom user control via the front end of a page/control in ASP.Net?

I have a custom user control that I am adding to a page via the front end (not back). 我有一个自定义用户控件,该控件是通过前端(而不是后端)添加到页面的。 That customer user control has a property that is of the type of a custom class. 该客户用户控件具有定制类类型的属性。 I would like to set that property via the declaration of the user control on the front end. 我想通过前端用户控件的声明来设置该属性。

Here's some code to help you understand what I'm talking about: 这是一些代码,可帮助您了解我在说什么:

In the custom user control .cs: 在自定义用户控件.cs中:

public BaseTUDSectionControl parentSectionControl
{
   get;
   set;
}

And at the declaration of an instance of that user control: 在该用户控件实例的声明中:

<tud:TUDDropDown ID="tddAccountExecutive" runat="server" />

I would like to set the parentSectionControl property in the same line, like this: 我想在同一行中设置parentSectionControl属性,如下所示:

<tud:TUDDropDown parentSectionControl=someClassInstance
       ID="tddAccountExecutive" runat="server" />

where 'someClassInstance' is a class instance that does exist... yeah. “ someClassInstance”是确实存在的类实例……是的。 I think I am going about this the wrong way, but I don't know how else to accomplish this. 我认为这是错误的做法,但是我不知道该怎么做。 I don't want to do it on the back end for maintenance reasons as I'll have hundreds of similar user controls added all throughout my project. 由于维护原因,我不想在后端执行此操作,因为在整个项目中都会添加数百个类似的用户控件。

Is anything like this possible, or am I smoking something? 这样可能吗,或者我在抽烟吗?

I'm not sure if/when Microsoft added this functionality to ASP.NET, but it is in fact possible to set a non-basic-type property in code-front: 我不确定是否/何时将此功能添加到ASP.NET,但是实际上可以在代码前设置非基本类型的属性:

TestPage.aspx: TestPage.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage" %>

<%@ Register Src="~/Control.ascx" TagPrefix="custom" TagName="Control" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Test Page</title></head>
<body>

    <custom:Control ID="TestControl" Items='<%# Items %>' runat="server" />

</body>
</html>

TestPage.aspx.cs: TestPage.aspx.cs:

using System;
using System.Collections.Generic;
using System.Web.UI;

public partial class TestPage : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TestControl.DataBind();
    }

    protected List<string> Items = new List<string>() { "Hello", "world!", };
}

Control.ascx: Control.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Control.ascx.cs" Inherits="Control" %>

<asp:Repeater ID="Repeater" ItemType="System.string" runat="server">
    <ItemTemplate>
        <p><%# Item %></p>
    </ItemTemplate>
</asp:Repeater>

Control.ascx.cs: Control.ascx.cs:

using System;
using System.Collections.Generic;
using System.Web.UI;

public partial class Control : UserControl
{
    public List<string> Items { get; set; }

    protected override void OnPreRender(EventArgs e)
    {
        Repeater.DataSource = Items;
        Repeater.DataBind();
    }
}

Note: it's important to bind the control from the page which sets the property . 注意: 从设置属性的页面绑定控件很重要。 So you still need a line in code-behind, but you still reap the readability benefits of setting the property in code-front. 因此,您仍然需要在代码隐藏行中添加一行,但是您仍然可以获得在代码前端设置属性的可读性。

No that's not possible. 不,那是不可能的。 You can only set property of basic types (int, string, bool) etc. from the markup (what you call front-end). 您只能从标记(称为前端)中设置基本类型(int,string,bool)等的属性。 If you have a property which is to be set to an instance of a class, this has to be done in the code-behind (what you call back-end). 如果您有一个要设置为类实例的属性,则必须在后台代码(您称为后端)中完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以从自定义用户控件继承现有的asp.net控件吗? - Can I inherit from an existing asp.net control from a custom user control? 将参数传递给用户控件 - asp.net - Pass parameters to a user control - asp.net ASP.NET Web Forms:如何将列表传递给用户控件? - ASP.NET Web Forms: How can I pass a list to a user control? 如何从ASP.NET网站(而不是Web应用程序)中的用户控件获取父页面 - How can I get the parent page from a User Control in an ASP.NET Website (not Web Application) 创建页面的另一个实例时,不会重新初始化asp.net页面上的用户控件 - User Control on a asp.net page doesn't get reinitialized when another instance of the page is created 如何在用户控件中访问此asp.net Panel控件并使之可见? - How can I access this asp.net Panel control in a user control and make it visible? 如何在ASP.NET C#中将数据从页面传递到用户控件,反之亦然? - How to pass data from page to user control and vise versa in asp.net C#? 如何将方法名称传递给asp.net中的自定义服务器控件? - How to pass method name to custom server control in asp.net? Asp.net自定义控件将对象作为属性传递 - Asp.net Custom Control pass object as a property Asp.Net WebForms-如何将ViewData作为参数传递给用户控件 - Asp.Net WebForms - How to pass ViewData as param to User Control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM