0

I have a custom usercontrol that has some Asp.net code. I would like to write the same code but with C#. The problem is that i don't know how to put a repeater and some buttons into the ContentTemplate.

The Asp.net Code :

<asp:UpdatePanel runat="server" ID="up">

    <ContentTemplate>

        <n2:Repeater ID="rpt" runat="server">
            <ItemTemplate></ItemTemplate>
        </n2:Repeater>

         <asp:LinkButton runat="server" ID="btnFirst" 
           Visible="false" Enabled="false" Text="<<" OnClick="btnFirst_Click" />

    </ContentTemplate>

</asp:UpdatePanel>

So how could I write this chunk in C# code? To be precise, how could I insert the repeater and the Linkbutton into the ContentTemplate.

Note : I don't want to use the LoadTemplate to do it.

Edit

I have tried ContentTemplateContainer.Controls.Add():

    private UpdatePanel up = new UpdatePanel();
    private Repeater rpt = Repeater();;

    public Paging{      

    //Add repeater to updatePanel
    up.ContentTemplateContainer.Controls.Add(rpt);


     AsyncPostBackTrigger apb3 = new AsyncPostBackTrigger();
     apb3.ControlID = "btnFirst";
     apb3.EventName = "Click";

     //Add Triggers to updatePanel
     up.Triggers.Add(apb1);

      //Create buttons
      btnFirst = new LinkButton();
      btnFirst.Visible = false;
      btnFirst.Enabled = false;
      btnFirst.Text = "<<";
      btnFirst.Click += new EventHandler(btnFirst_Click);

     //Add buttons to update panel
       up.ContentTemplateContainer.Controls.Add(btnFirst);

     }

     protected void Page_Load(object sender, EventArgs e)
     {
        rpt.ItemTemplate = LoadTemplate("~/UI/Templates/NewsEvent.ascx");
       ....
     }

I have an error caused by the first line on Page_Load : Databinding methods such as Eval(), XPath(), and Bind() can only be used in controls contained in a page.

This is NewsEvent.ascx:

<img src='<%# Eval("ImageThumbnail") %>' alt="" />
3
  • What exactly you need? What have you tried? Commented Jul 8, 2011 at 8:03
  • @VMAtm, please see the Edit question's part. Commented Jul 8, 2011 at 8:26
  • NewsEvent.ascx this controls have some error, you used Eval or bind method in the control which is used database connection. Commented Jul 8, 2011 at 8:39

1 Answer 1

1

you cant do this with ITemplate types ... i had a similar problem trying to clone a tabpanel in a tabcontainer control ... i already had a hidden tabpanel and all i wanted to do was create a new tabpanel and basically instantiate the ITemplate from the hidden one in the new one.

The problem is ITemplate ... it's not very dynamic for code behind interactions i would suggest putting that markup on the page as you already have and setting visible = false on the parent then when you need to databind and show the hidden panel.

getting the initial bind to work isn't the problem ... its the postback handling ...

Ajax TabContainerTabPanels Break postbacks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.