[英]“Control with id could not be located or a different control is assigned to the same ID after postback.” Error
[英]Different control is assigned to the same ID after postback - ASP.NET
我最近将一个旧的ASP.NET应用程序从.NET 2.0更新到了.NET 4.5,这是我尝试使用asp:UpdatePanel
控件的第一个应用程序之一。 它是一个调查应用程序,用户可以在其中创建调查,为调查分配多个问题组,并为每个问题组分配多个问题类型。
当我使用页面用户可以编辑包含已注册到LinkButton
asp.net控件的AsyncPostBackTrigger
对象的调查内容的页面时,页面上注册的问题类型为用户控件,我收到以下错误消息:
错误:Sys.WebForms.PageRequestManagerServerErrorException:发生错误是因为无法找到ID为'ctl00 $ PageBody $ gvSurveyQuestions $ ctl02 $ ctl03'的控件,或者在回发后将另一个控件分配给了相同的ID。 如果未分配ID,请显式设置引发回发事件的控件的ID属性,以避免此错误。
用户控件是根据用户选择哪个控件作为组的一部分而动态生成的。 在DataGrid
控件的OnRowCommand
事件中,它根据所选问题的类型确定要显示的用户控件。 这是一段代码:
调查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);
}
为什么我会收到此错误?寻找在哪里进行修复的一些好的建议是什么?
切换到.Net Framework 4.0时,带有多个CommandField的Gridview似乎无法正常工作。 我猜是因为不能在Gridview中给命令字段一个ID,所以在这种情况下,编译器会在内部为第一个命令字段分配一个控件ID,当遇到第二个命令字段时,它会再次分配相同的控件ID。 因此,在回发期间,当我尝试重新加载gridview时,例如说编辑一行时,它遇到具有相同id的多个控件,并引发此服务器错误。
我删除了命令字段,而是用TemplateField内的Imagebutton替换了它们,从而解决了我的问题。 :)
礼貌此职位
通过将asp:CommandField
替换为Edit和Delete链接的asp:ButtonField
并将CommandName
从Edit重命名为Edt并将Delete重命名为Del,我能够解决我的问题
这篇文章最终帮助解决了这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.