Only one instance of a ScriptManager can be added to the page in asp.net

穿精又带淫゛_ 提交于 2021-02-16 13:08:46

问题


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

<%@ 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);
        }
    }

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 enter image description here

Any ideas? Thanks in advance


回答1:


Try removing ScriptManager from user control.

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>

In case you require ScriptManager on Master page,In this case, there is NO need to have a ScriptManager in userControl. Follow below rules:

  • 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

Below is what MSDN says about ScriptManager Control.

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




回答2:


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.

Remove Script Manager from User Control, It should work then.

Take a look here




回答3:


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




回答4:


If using a master page and AjaxControlToolkit doesn't work inside,then replace:

<asp:ScriptManager ID="smContent" runat="server">
</asp:ScriptManager>

With something like this:

<AjaxToolkit:ToolkitScriptManager ID="smContent" runat="server">
</AjaxToolkit:ToolkitScriptManager>

http://forums.asp.net/t/1923659.aspx?Using+ScriptManager+or+ToolkitScriptManager+with+ASP+NET+3+5+AJAX+Website



来源:https://stackoverflow.com/questions/19087044/only-one-instance-of-a-scriptmanager-can-be-added-to-the-page-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!