use*_*094 5 c# asp.net ajax user-controls
当我尝试添加用户控件时,我收到了此错误
Only one instance of a ScriptManager can be added to the page
Run Code Online (Sandbox Code Playgroud)
码:
的.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisaUserControl.ascx.cs" Inherits="Portal.VisaUserControl" %>
<%@ Register Assembly="BasicFrame.WebControls.BasicDatePicker" Namespace="BasicFrame.WebControls" TagPrefix="dp" %>
<asp:ScriptManager ID="scriptmanager1" runat="server"></asp:ScriptManager>
<div id="divreg" runat="server">
<table id="tbl" runat="server">
<tr>
<td>
<asp:Label ID="lbl2" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td> Visa Number:</td>
<td><asp:TextBox ID="txtUser" Width="160px" runat="server"/></td>
<td> Country Name:</td>
<td><asp:DropDownList ID="dropCountry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td> Type of Visa:</td>
<td><asp:DropDownList ID="dropVisa" Width="165px" runat="server" OnSelectedIndexChanged="dropVisa_SelectedIndexChanged"></asp:DropDownList></td>
<td> Type of Entry:</td>
<td><asp:DropDownList ID="dropEntry" Width="165px" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td> Expiry Date</td>
<td><asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server"
TargetControlID="txtDate" PopupButtonID="Imgbtnfromdate" Format="dd/MM/yyyy">
</ajaxToolkit:CalendarExtender>
</td>
<td>
<asp:Button ID="btnRemove" Text="Remove" runat="server" OnClick="btnRemove_Click" />
</td>
</tr>
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
.aspx.cs
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
GenerateControls();
}
private void GenerateControls()
{
foreach (string i in NoOfControls)
{
VisaUserControl ctrl = (VisaUserControl)Page.LoadControl("VisaUserControl.ascx");
ctrl.ID = i;
this.rpt1.Controls.Add(ctrl);
}
}
Run Code Online (Sandbox Code Playgroud)
这是问题所在
protected void btnAddVisa_Click(object sender, EventArgs e)
{
List<string> temp = null;
var uc = (VisaUserControl)this.LoadControl(@"VisaUserControl.ascx");
string id = Guid.NewGuid().ToString();
uc.ID = id;
temp = NoOfControls;
temp.Add(id);
NoOfControls = temp;
rpt1.Controls.Add(uc);
}
Run Code Online (Sandbox Code Playgroud)
在下图中,如果单击添加按钮,我收到此错误

有任何想法吗?提前致谢
R.C*_*R.C 24
尝试ScriptManager从用户控件中删除.
您肯定已在.aspxPage或MasterPage中的其他位置添加了ScriptManager .检查这些页面.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
Run Code Online (Sandbox Code Playgroud)
如果您需要ScriptManager在母版页上,在这种情况下,不需要在userControl中有ScriptManager.遵循以下规则:
<asp:Scriptmanager>在母版页上只有一个.<asp:ScriptManagerProxy>在每个可能需要脚本管理器的内容页面上也有一个且只有一个以下是MSDN关于ScriptManager控制的内容.
只能将一个ScriptManager控件实例添加到页面中.页面可以直接包含控件,也可以间接包含在嵌套组件中,例如用户控件,母版页的内容页面或嵌套母版页.如果页面已包含ScriptManager控件,但嵌套或父组件需要ScriptManager控件的其他功能,则该组件可以包含ScriptManagerProxy控件.例如,ScriptManagerProxy控件使您可以添加特定于嵌套组件的脚本和服务