How to grab a reference collection like the Table class?

How do I capture controls inside a UserControl tag?

So, if this is on the page:

<ME:NewControl ID="tblCustomList" runat="server">
// ... these controls will be used in my UserControl.aspx
</ME:NewControl>

      

How do I access these controls in the UserControl?

For example, the Table class does the following:

<asp:Table ID="tblNormal" runat="server">
  <asp:TableRow>
      <asp:TableCell>Thing 1</asp:TableCell>
      <asp:TableCell>Thing 2</asp:TableCell>
  </asp:TableRow>
</asp:Table>

      

I am getting a message that my UserControl "does not have a public property named" TableRow "when I do this:

<ME:NewControl ID="tblCustomList" runat="server">
  <asp:TableRow>
      <asp:TableCell>Thing 1</asp:TableCell>
      <asp:TableCell>Thing 2</asp:TableCell>
  </asp:TableRow>
</ME:NewControl>

      

I found this example to help extend the Table class, but that's not exactly what I want to do.

I also found this description on how to use Templated Controls, which I'm not sure if I can use.

0


source to share


4 answers


It turns out I just need something like this:

public TableRow DTHeader { get; set; }

protected override void OnInit(EventArgs e)
{
    tblCollection.Rows.Add(DTHeader);
    base.OnInit(e);
}

      



And then use this in my page:

<ME:NewControl ID="tblCustomList" runat="server">
    <DTHeader>
        <asp:TableCell></asp:TableCell>
        //...
    </DTHeader>
</ME:NewControl>

      

+1


source


I'm not sure if I understand the question 100%.

However, if you are customizing the Row control, you can add a collection to the custom master table control.



You can do this with a generic list. This should give you the ability to add them like a normal aspx control.

I don't have any sample code here, but I've done it many times.

0


source


I'm not sure I understand your question.

You can access the server on the control side simply by using tblCustomList.Controls and then you can loop over and add to your custom control. But then again, I'm not sure what you are asking.

0


source


the pattern you referenced seems to be what you are trying to accomplish.
Have you added the following to your code?

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
    [AspNetHostingPermission(SecurityAction.Demand, 
        Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class CustomTableCreateControlCollection : Table
    {
        protected override ControlCollection CreateControlCollection()
        {
            // Return a new ControlCollection
            return new ControlCollection(this);
        }
    }
}

      

0


source







All Articles