Hi I am trying to use AjaxControlToolkit's Accordion control in user control. That user control is added to page dynamically from code behind. My code for user control is as follows:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs"
Inherits="Project.UC.Control" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
TagPrefix="asp" %>
<asp:Accordion ID="Accordion1" runat="server" HeaderSelectedCssClass="Sel"
HeaderCssClass="unSel" SelectedIndex="0">
<Panes>
<asp:AccordionPane runat="server" ID="AccordionPane1">
<Header>Red Orchid</Header>
<Content>Some content here.</Content>
</asp:AccordionPane>
</Panes>
</asp:Accordion>
To add user control dynamically I used following code in my page's code behind
Control mycontrol = this.LoadControl("~/myUserControl.ascx");
this.Controls.Add(mycontrol);
On my user control consumer page I have form tag with runat="server" also added ScriptManager control for ajax functionality. When I run my code I am getting following error
Control 'ctl02_Accordion1_AccordionExtender' of type
'AccordionExtender' must be placed inside a form tag with runat=server.
I already used form with runat="server" tag on my consumer page why this problem is coming. To solve this problem I moved my consumer page's form tag to user control this solves my problem but by doing this I can't take advantage of postback on my consumer page. If I put form tag with runat="server" in both user control and consumer page it show error that you can use only one form tag on page. How to solve this problem.
Thanks in advance..
Add a PlaceHolder
control onto a form and instead of this.Controls.Add(mycontrol);
use PlaceHolder1.Controls.Add(mycontrol);
Or you need to add controls to form's Controls
collection. In assumption that form has aspnetForm
id you should write: aspnetForm.Controls.Add(mycontrol);
The reason is that Page
class has own Controls
property thus this.Controls.Add(mycontrol);
line add control to Page.Controls collection. But all controls those submit any data to server must be placed in form
element otherwise theirs values shouldn't be accessible on server side. This follows from html forms mature: HTML Forms and Input