2013-02-14 51 views
0

嗨我想在用戶控件中使用AjaxControlToolkit的Accordion控件。該用戶控件從後面的代碼中動態添加到頁面中。我的用戶控件的代碼如下:使用AjaxControlToolkit在用戶控件中的Accordion控件動態地添加到頁面在asp.net

<%@ 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> 

動態地添加用戶控件我曾經跟隨我的網頁的代碼後面的代碼

Control mycontrol = this.LoadControl("~/myUserControl.ascx"); 
this.Controls.Add(mycontrol); 

在我的用戶控制消費的網頁我有形式標記與RUNAT = 「服務器」還爲Ajax功能添加了ScriptManager控件。當我運行我的代碼我收到以下錯誤

Control 'ctl02_Accordion1_AccordionExtender' of type 
'AccordionExtender' must be placed inside a form tag with runat=server. 

我我的消費者頁面上RUNAT =「服務器」標籤已經使用的形式,爲什麼這個問題來了。爲了解決這個問題,我將我的消費者頁面的表單標籤移動到用戶控件上,這樣可以解決我的問題,但通過這樣做我無法利用我的消費者頁面上的回發。如果我在用戶控件和消費者頁面中都將runat =「server」的表單標籤放在表單中,則表明您只能在頁面上使用一個表單標籤。如何解決這個問題呢。

在此先感謝..

回答

0

添加PlaceHolder控制到形式和替代this.Controls.Add(mycontrol);使用PlaceHolder1.Controls.Add(mycontrol);

,或者需要添加控件到窗體的Controls集合。在假設的形式有aspnetForm ID,你應該寫:aspnetForm.Controls.Add(mycontrol);

的原因是Page類具有自主Controls財產從而this.Controls.Add(mycontrol);行添加控件Page.Controls集合。但是,向服務器提交任何數據的所有控件都必須放置在form元素中,否則它們的值不應在服務器端訪問。這從成熟的html表格來:HTML Forms and Input

+0

嘿謝謝你的回覆。放置佔位符或面板並添加用戶控件以解決我的問題,但是仍然可以請解釋爲什麼將用戶控件添加到佔位符或面板獲取表單標記並將用戶控件添加到頁面控件集合沒有獲取表單標記並顯示錯誤? – CodeWarrior 2013-02-14 09:52:39

+0

@CodeWorrier我已經更新我的回答 – 2013-02-14 10:32:17

+0

感謝您的重播! – CodeWarrior 2013-02-15 06:30:59