问题描述
当我尝试添加用户控件时出现此错误
When I'm trying to add user control I got this error
Only one instance of a ScriptManager can be added to the page
代码:
.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>
.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);
}
}
问题来了
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);
}
在下图中,如果单击添加按钮出现此错误
In the below image if Click add button I got this error
有什么想法吗?提前致谢
Any ideas? Thanks in advance
推荐答案
尝试从用户控件中删除 ScriptManager
.
Try removing ScriptManager
from user control.
您肯定在 .aspx
页面或 MasterPage 的其他位置添加了 ScriptManager.检查这些页面.
You have definitely added a ScriptManager somewhere else in your .aspx
Page or MasterPage. Check these pages.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
如果您在母版页上需要 ScriptManager
,在这种情况下,用户控件中不需要有 ScriptManager.请遵守以下规则:
In case you require ScriptManager
on Master page,In this case, there is NO need to have a ScriptManager in userControl. Follow below rules:
- 确保母版页上只有一个
<asp:Scriptmanager>
. - 确保在每个可能需要脚本管理器的内容页面上也只有一个
<asp:ScriptManagerProxy>
以下是 MSDN
谈到 ScriptManager
控制.
Below is what MSDN
says about ScriptManager
Control.
只能将 ScriptManager 控件的一个实例添加到页.页面可以直接或间接包含控件在嵌套组件内,例如用户控件、内容页面母版页或嵌套母版页.如果页面已经包含ScriptManager 控件,但需要嵌套或父组件ScriptManager 控件的附加功能,该组件可以包括一个 ScriptManagerProxy 控件.例如,ScriptManagerProxy 控件使您能够添加脚本和服务特定于嵌套组件
Only one instance of the ScriptManager control can be added to the page. The page can include the control directly, or indirectly inside a nested component such as a user control, content page for a master page, or nested master page. If a page already contains a ScriptManager control, but a nested or parent component needs additional features of the ScriptManager control, the component can include a ScriptManagerProxy control. For example, the ScriptManagerProxy control enables you to add scripts and services that are specific to nested components
这篇关于在 asp.net 的页面中只能添加一个 ScriptManager 实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!