我有以下情况:
我有一个包含listview占位符的页面,其中包含动态添加到页面的控件。我可以通过postBack更改这些控件,其中交换占位符。但是,在postBack更改整个控件之前的某些控件与占位符的新内容中的某些控件具有相同的ID。 这会导致PageRequestManagerServerErrorException:无效的回发或回调参数。
我该怎么做才能避免ID导致此异常。
我不想关闭事件验证,不幸的是,无法更改控件的ID。
感谢您的帮助!
<asp:ListView ID="rows" runat="server" OnSorting="rows_sort" OnItemCreated="rows_created" OnDataBound="rows_databound">
<LayoutTemplate>
<div>
<table id="editGrid">
<thead>
<asp:PlaceHolder ID="header" runat="server"></asp:PlaceHolder>
</thead>
<tbody>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server"></asp:PlaceHolder>
</tbody>
</table>
</div>
</LayoutTemplate>
<ItemTemplate>
<tr><asp:PlaceHolder ID="columns" runat="server"></asp:PlaceHolder></tr>
</ItemTemplate>
<EmptyDataTemplate>
<asp:PlaceHolder ID="noData" OnInit="noData_Init" runat="server"></asp:PlaceHolder>
</EmptyDataTemplate>
</asp:ListView>
创建控件的代码(其中一些控件可能具有相同的ID)。
protected void noData_Init(object sender, EventArgs e)
{
PlaceHolder phNoDat = (PlaceHolder)sender;
System.Web.UI.WebControls.Table tbl = new System.Web.UI.WebControls.Table();
tbl.ID = "editGrid";
tbl.ClientIDMode = System.Web.UI.ClientIDMode.Static;
TableHeaderRow thr, thrFilter;
buildHeader(out thr, out thrFilter); // Create filter controls (e.g. dropdown, textbox) which are then added to the rows
tbl.Controls.Add(thr);
tbl.Controls.Add(thrFilter);
phNoDat.Controls.Add(tbl);
topToolBar.Visible = true;
}
buildHeader方法:
private void buildHeader(out TableHeaderRow thr, out TableHeaderRow thrFilter)
{
thr = new TableHeaderRow();
thrFilter = new TableHeaderRow();
thrFilter.ID = "thFilterRow";
TableHeaderCell thc = new TableHeaderCell();
thc.Height = new Unit("32px");
LinkButton lb = new LinkButton();
lb.ID = "header_" + ColumnName;
lb.CommandName = "Sort";
lb.CommandArgument = ColumnName;
lb.Text = "";
Literal lit = new Literal();
lit.Text = "<div style=\"min-width: " + colWidth + "px\"><div class=\"pull-left\">" + DisplayName + "</div><i class=\"" + sortIcon + " pull-right\" style=\"padding-top: 3px;\"> </i></div>";
lb.Controls.Add(lit);
thc.Controls.Add(lb);
thr.Controls.Add(thc);
DropDownList cbValSel;
HiddenField hfIsLookup;
string def = null;
/* ... */
thcFilter.Controls.Add(cbValSel);
thrFilter.Controls.Add(thcFilter);
/* ... */
}