【问题标题】:Different control is assigned to the same ID after postback - ASP.NET回发后将不同的控件分配给相同的ID - ASP.NET
【发布时间】:2013-12-09 21:02:50
【问题描述】:

我最近将我的一个旧 ASP.NET 应用程序从 .NET 2.0 更新到 .NET 4.5,它是我尝试使用 asp:UpdatePanel 控件的首批应用程序之一。这是一个调查应用程序,用户可以在其中创建调查,将多个问题组分配给调查,并将多个问题类型分配给每个问题组。

当我使用用户可以编辑包含AsyncPostBackTrigger 对象的调查内容的页面时,这些对象已注册到LinkButton asp.net 控件问题类型的用户控件在页面上注册,我收到以下错误:

错误:Sys.WebForms.PageRequestManagerServerErrorException:错误 发生是因为一个带有 id 的控件 找不到“ctl00$PageBody$gvSurveyQuestions$ctl02$ctl03”或 回发后将不同的控件分配给相同的 ID。如果 ID 未分配,显式设置控件的 ID 属性 引发回发事件以避免此错误。

用户控件是根据用户选择哪个控件作为组的一部分动态生成的。在DataGrid 控件的OnRowCommand 事件中,它根据所选问题的类型确定要显示哪个用户控件。这是一段sn-p代码:

调查 ASPX 页面:

//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>

ASPX 页面背后的代码:

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

用户控件后面的代码将链接按钮设置为调查 ASPX 页面上的回发触发器:

public event EventHandler lbCreateUpdateQuestionClick;

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

为什么我会收到这个错误,有什么好的建议可以帮助我解决这个问题?

【问题讨论】:

    标签: c# asp.net user-controls updatepanel asyncpostbackerror


    【解决方案1】:

    似乎带有多个 CommandField 的 Gridview 在切换时不起作用 到 .Net 框架 4.0。我想这是因为一个人不能 给gridview内的命令字段一个id,所以在这种情况下 编译器在内部为第一个命令字段分配一个控件 ID 当它遇到第二个时,它会分配相同的控件 ID 再次。所以在回发期间,当我尝试重新加载 gridview 时说 编辑一行,它会遇到多个具有相同 id 的控件并且 引发此服务器错误。

    我已经删除了命令字段,而是将它们替换为 TemplateField 中的 Imagebutton 解决了我的问题。 :)

    感谢This post

    【讨论】:

    • 我的页面上有两个 asp:GridView 控件,其中包含 asp:CommandField 列。将重构到 asp:ButtonField 看看是否能解决我的问题。
    • asp:CommandField 替换为asp:GridView 控件中的两个asp:ButtonField 列时仍然出现相同的错误
    • 我将能够使用这篇文章中提供的答案解决我的问题:stackoverflow.com/a/18567488/26327
    【解决方案2】:

    我能够通过将编辑和删除链接的 asp:CommandField 替换为 asp:ButtonField 并将 CommandNameEdit 重命名为 Edt 来解决我的问题和 DeleteDel

    post 终于解决了这个问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多