سؤال

I am trying to achieve something like this in a server control.

<MyNewControl runat="server">
    <FirstTemplate>
        <asp:Label runat="server" ForeColor="Red">Hello</asp:Label>
    </FirstTemplate>
    <SecondTemplate>
        <asp:Label runat="server" ForeColor="Blue">Hello</asp:Label>
    </SecondTemplate>
</MyNewControl>

I'm hoping to know how to implement that MyNewControl server control to accept those template containers. I'd like them to allow more complex content than just containing a label.
The goal is to be able to choose which template I want the control to render out.

I've looked around and I couldn't find something that tells me what part of ASP.NET that supports what I'm looking for.

هل كانت مفيدة؟

المحلول

If the goal is to choose which piece of content to render, you could use the built-in ASP.NET MultiView control instead of writing your own.

e.g. ASPX Markup:

<asp:MultiView ID="multiExample" runat="server">
  <asp:View ID="viewOne" runat="server">
    <h1>This is my first view</h1>
  </asp:View>
  <asp:View ID="viewTwo" runat="server">
    <h1>This is my second view</h1>
  </asp:View>
</asp:MultiView>

C# Code-behind code for switching between "templates":

  if (condition1)
  {
    multiExample.SetActiveView(viewOne);
  }
  else
  {
    multiExample.SetActiveView(viewTwo);
  }

If you'd like to learn how to create a templated user control from scratch, there's an MSDN tutorial here: http://msdn.microsoft.com/en-us/library/36574bf6(v=vs.100).aspx

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top