简体   繁体   中英

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:

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:

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

where 'someClassInstance' is a class instance that does exist... yeah. 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:

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:

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

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). 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM