Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 I click the Add button, I get the error:

enter image description here

like image 784
user2500094 Avatar asked Sep 30 '13 05:09

user2500094


1 Answers

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

like image 100
R.C Avatar answered Nov 15 '22 17:11

R.C