Different control is assigned to the same ID after postback - ASP.NET

3.9k views Asked by At

I have recently updated one of my old ASP.NET applications from .NET 2.0 to .NET 4.5 and it was one of the first applications where I attempted to use the asp:UpdatePanel control. It is a survey application where a user can create a survey, assigned multiple question groups to the survey and multiple questions types to each question group.

When I use the page where a user can edit the survey content that contains AsyncPostBackTrigger objects that are registered to LinkButton asp.net controls in question type user controls registered on the page, I get the following error:

Error: Sys.WebForms.PageRequestManagerServerErrorException: An error has occurred because a control with id 'ctl00$PageBody$gvSurveyQuestions$ctl02$ctl03' could not be located or a different control is assigned to the same ID after postback. If the ID is not assigned, explicitly set the ID property of controls that raise postback events to avoid this error.

The user control is dynamically generated based on which control the user selected to be part of the group. In the OnRowCommand event of the DataGrid control, it determined which user control to display based on the type of question selected. Here is a snippet of code:

survey ASPX page:

//grid control to allow user to select the question to edit
<asp:GridView ID="gvSurveyQuestions" runat="server" AutoGenerateColumns="false" 
    OnRowCommand="gvSurveyQuestions_OnRowCommand" 
    OnRowEditing="gvSurveyQuestions_OnRowEditing" 
    OnRowDeleting="gvSurveyQuestions_OnRowDeleting" 
    CssClass="com_grid"
    Width="100%" 
    CellPadding="3" 
    CellSpacing="0"
>
    <asp:CommandField ButtonType="Link" ShowEditButton="True" ShowDeleteButton="True" 
        ItemStyle-HorizontalAlign="Center" />
</asp:GridView>
<asp:PlaceHolder ID="phEditExcellentPoor" runat="server" Visible="false">
    <tr>
        <td width="100%">
            <uc:ExcellentPoor Id="ucEditExcellentPoor" runat="server" EditMode="Edit" />
        </td>
    </tr>
</asp:PlaceHolder>

Code Behind of ASPX page:

private readonly string m_CreateUpdateQuestionControlName = "lbCreateUpdateQuestion";
private readonly string m_CreateUpdateQuestionText_Edit = "Update Question";

protected void Page_Init(object sender, EventArgs e)
{
    //here is the code to set the async postback trigger
    AsyncPostBackTrigger apExcellentPoorEdit = new AsyncPostBackTrigger();
    apExcellentPoorEdit.ControlID = ucEditExcellentPoor.FindControl(this.m_CreateUpdateQuestionControlName).UniqueID;
    upSurveyQuestions.Triggers.Add(apExcellentPoorEdit);
}

Code behind of user control to set link button as postback trigger on the survey ASPX page:

public event EventHandler lbCreateUpdateQuestionClick;

protected void lbCreateUpdateQuestion_OnClick(object sender, EventArgs e)
{
    if (this.lbCreateUpdateQuestionClick != null)
        this.lbCreateUpdateQuestionClick(sender, e);
}

Why I am getting this error and what would be some good suggestions to look where to fix it?

2

There are 2 answers

3
geedubb On

Seems Gridview with multiple CommandField is not working when switched to .Net Framework 4.0. I guess its because of the fact that one cant give an id to command field inside the gridview, so in this case the compiler internally assigns a control id for the first command field and when it encounters the second it assigns the same control id again. So during postback when i try to reload the gridview say when editing a row, it encounters multiple controls with the same id and throws this server error.

I have removed the command fields and instead replaced them with Imagebutton inside TemplateField which has resolved my issue. :)

Courtesy of This post

0
Michael Kniskern On

I was able to resolve my issue by replacing the asp:CommandField with asp:ButtonField for the Edit and Delete links and renaming the CommandName from Edit to Edt and Delete to Del

This post helped finally resolve the issue