Я хочу создать управление точно так же, как a Panel
.
Я хочу, чтобы мое управление приняло некоторые средства управления как childs, не вводя шаблонное имя, точно так же, как Panel
, как показано здесь:
<asp:Panel runat="server">
My content
<div>Content</div>
</asp:Panel>
У меня есть средства управления с содержанием внутри, не говоря то, что ITemplate
.
Я в основном хочу преобразовать это
<my:MyControl runat="server">
<ContentTemplate>
My content
<div>Content</div>
</ContentTemplate>
</my:MyControl>
В это
<my:MyControl runat="server">
My content
<div>Content</div>
</my:MyControl>
Вот то, что я имею:
public class MyControl : CompositeControl
{
[TemplateInstance(TemplateInstance.Single)]
public ITemplate Content { get; set; }
protected override void CreateChildControls()
{
base.CreateChildControls();
Content.InstantiateIn(this);
}
}
Вышеупомянутые работы с <Content></Content>
теги в управлении, но без него не работает. И атрибут не делает ничего вообще (я предполагаю). Что отсутствует?
Как я могу достигнуть его? Какие-либо подсказки? Почему делает Panel
поддерживать это?
Привет, Бруно, ответ не имеет ничего общего с пользовательскими элементами управления с точки зрения того, как вы хотите вкладывать элементы управления.
На самом деле это связано с шаблонами и шаблонами. Вот как серверные элементы управления могут иметь вложенные в них определенные элементы управления.
Это , обычно , которое можно найти в элементах управления привязкой данных.
Ответ, к сожалению, нетривиален, но вы можете найти полное руководство здесь
Хм .... Я думаю, что это просто усложняет ситуацию. Что мешает вам создать настраиваемый элемент управления, вложить элементы управления в него, а затем получить доступ к этому элементу управления через свойство ControlCollection родительского элемента управления?
Создайте настраиваемый пользовательский элемент управления, а затем используйте его для дальнейшего расширения дочерних элементов управления.
http://www.akadia.com/services/dotnet_user_controls.html
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace Akadia
{
namespace SubmitButton
{
// User Control which contain a text box for your
// name and a button that will fire an event.
public class SubmitButtonControl : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Label lblName;
private System.Windows.Forms.Button btnSubmit;
private System.ComponentModel.Container components = null;
// Declare delegate for submit button clicked.
//
// Most action events (like the Click event) in Windows Forms
// use the EventHandler delegate and the EventArgs arguments.
// We will define our own delegate that does not specify parameters.
// Mostly, we really don't care what the conditions of the
// click event for the Submit button were, we just care that
// the Submit button was clicked.
public delegate void SubmitClickedHandler();
// Constructor public SubmitButtonControl()
{
// Create visual controls
InitializeComponent();
}
// Clean up any resources being used.
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
.....
.....
// Declare the event, which is associated with our
// delegate SubmitClickedHandler(). Add some attributes
// for the Visual C# control property.
[Category("Action")]
[Description("Fires when the Submit button is clicked.")]
public event SubmitClickedHandler SubmitClicked;
// Add a protected method called OnSubmitClicked().
// You may use this in child classes instead of adding
// event handlers.
protected virtual void OnSubmitClicked()
{
// If an event has no subscribers registerd, it will
// evaluate to null. The test checks that the value is not
// null, ensuring that there are subsribers before
// calling the event itself.
if (SubmitClicked != null)
{
SubmitClicked(); // Notify Subscribers
}
}
// Handler for Submit Button. Do some validation before
// calling the event.
private void btnSubmit_Click(object sender, System.EventArgs e)
{
if (txtName.Text.Length == 0)
{
MessageBox.Show("Please enter your name.");
}
else
{
OnSubmitClicked();
}
}
// Read / Write Property for the User Name. This Property
// will be visible in the containing application.
[Category("Appearance")]
[Description("Gets or sets the name in the text box")]
public string UserName
{
get { return txtName.Text; }
set { txtName.Text = value; }
}
}
}
}