C# 一个 ScriptManager 的实例只能添加到 asp.net 中的页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19087044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Only one instance of a ScriptManager can be added to the page in asp.net
提问by user2500094
When I'm trying to add user control I got this error
当我尝试添加用户控件时出现此错误
Only one instance of a ScriptManager can be added to the page
Code:
代码:
.ascx
.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
.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);
}
}
Here is the problem
这是问题所在
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
有任何想法吗?提前致谢
采纳答案by R.C
Try removing ScriptManager
from user control.
尝试ScriptManager
从用户控制中删除。
You have definitely added a ScriptManager somewhere else in your .aspx
Page or MasterPage. Check these pages.
您肯定已经在.aspx
页面或母版页的其他地方添加了 ScriptManager 。检查这些页面。
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp: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:
如果您需要ScriptManager
在母版页上,在这种情况下,用户控件中不需要 ScriptManager。遵循以下规则:
- Ensure you have one and only one
<asp:Scriptmanager>
on the masterpage. - Ensure you also have one and only one
<asp:ScriptManagerProxy>
on each and every content page that might require a script manager
- 确保您
<asp:Scriptmanager>
的主页上只有一个。 - 确保
<asp:ScriptManagerProxy>
在每个可能需要脚本管理器的内容页面上也只有一个
Below is what MSDN
says about ScriptManager
Control.
下面是MSDN
关于ScriptManager
控制的内容。
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
只能向页面添加 ScriptManager 控件的一个实例。该页面可以直接或间接包含在嵌套组件(例如用户控件、母版页的内容页或嵌套母版页)中的控件。如果页面已包含 ScriptManager 控件,但嵌套组件或父组件需要 ScriptManager 控件的附加功能,则该组件可以包含 ScriptManagerProxy 控件。例如,ScriptManagerProxy 控件使您能够添加特定于嵌套组件的脚本和服务
回答by Nikhil Chavan
Are you using Script Manager on Page where you are using this User Control ? You will get an error if your Parent Page where you are using User Control already has Script Manager defined.
您是否在使用此用户控件的页面上使用脚本管理器?如果您使用用户控件的父页面已经定义了脚本管理器,您将收到错误消息。
Best practice is to have ScriptManager on Master Page.
最佳做法是在母版页上安装 ScriptManager。
Remove Script Manager from User Control, It should work then.
从用户控制中删除脚本管理器,它应该可以工作。
回答by Nitu Bansal
it seems you are using scriptmanager on some place in your usercontrol.
您似乎在用户控件的某个地方使用了脚本管理器。
try to remove it, only the reason of this error is scriptmanager can not be defined more thane one time in page
尝试删除它,这个错误的唯一原因是scriptmanager不能在页面中定义超过一次
回答by user2648846
If using a master page and AjaxControlToolkit doesn't work inside,then replace:
如果使用母版页并且 AjaxControlToolkit 在其中不起作用,则替换:
<asp:ScriptManager ID="smContent" runat="server">
</asp:ScriptManager>
With something like this:
像这样:
<AjaxToolkit:ToolkitScriptManager ID="smContent" runat="server">
</AjaxToolkit:ToolkitScriptManager>