我正在尝试使用 ajax 工具包构建具有多个可折叠面板的动态页面。但是当页面加载时它给了我:
“找到具有相同 ID '_ClientState' 的多个控件。FindControl 要求控件具有唯一的 ID。”
(我在阅读本文中的技巧 5 后编写了代码)
我调试并检查了所有 ID 是否都是唯一的,但它仍然会发生。
我创建了一个代表候选人的类,并使用它从 ms sql 获取所有候选人并为每个候选人创建一个可折叠面板,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Web.Security;
using System.Data.SqlClient;
using AjaxControlToolkit;
public partial class member_pages_member_page1 : System.Web.UI.Page
{
dbservices db = new dbservices();
protected void Page_Load(object sender, EventArgs e)
{
//NameValueCollection coll = Request.QueryString;
//string s1 = coll["role"];
string [] role=Roles.GetRolesForUser();
if (role[0] == "secretary")
{
Table tbl = new Table();
TableRow row1 = new TableRow();
TableCell cell1 = new TableCell();
cell1.Text = "Member name";
TableCell cell2 = new TableCell();
cell2.Text = "Application date";
TableCell cell3 = new TableCell();
cell3.Text = "Days Waiting";
row1.Controls.Add(cell1);
row1.Controls.Add(cell2);
row1.Controls.Add(cell3);
tbl.Controls.Add(row1);
int i = 1;
List<candidate> candidate1 = db.return_secretary_candidates();
foreach (candidate c1 in candidate1)
{
DateTime today = DateTime.Today;
TimeSpan diff = today.Subtract(c1.app_date);
// Create Header Panel
Panel panelHead = new Panel();
panelHead.ID = "pH"+i.ToString();
panelHead.CssClass = "cpHeader";
// Add Label inside header panel to display text
Label lblHead = new Label();
lblHead.ID = "lblHeader"+i.ToString();
panelHead.Controls.Add(lblHead);
member m1 = db.return_member(c1.id);
//Create Body Panel
Panel panelBody = new Panel();
panelBody.ID = "pB"+i.ToString();
panelBody.CssClass = "cpBody";
// Add Label inside body Panel to display text
Table table1 = new Table();
table1.ID = "details_tbl"+i.ToString();
TableRow r1 = new TableRow();
TableRow r2 = new TableRow();
TableCell cell11 = new TableCell();
TableCell cell12 = new TableCell();
TableCell cell13 = new TableCell();
TableCell cell14 = new TableCell();
cell14.Text = m1.f_name + " " + m1.l_name;
cell13.Text = m1.prof.secondary;
cell12.Text = m1.experience.ToString();
cell11.Text = m1.exp_brief;
r1.Controls.Add(cell11);
r1.Controls.Add(cell12);
r1.Controls.Add(cell13);
r1.Controls.Add(cell14);
r2.Controls.Add(cell11);
table1.Controls.Add(r1);
table1.Controls.Add(r2);
panelBody.Controls.Add(table1);
// Create CollapsiblePanelExtender
CollapsiblePanelExtender cpe =new CollapsiblePanelExtender();
cpe.TargetControlID = panelBody.ID;
cpe.ExpandControlID = panelHead.ID;
cpe.CollapseControlID = panelHead.ID;
cpe.ScrollContents = false;
cpe.Collapsed = true;
cpe.ExpandDirection =
CollapsiblePanelExpandDirection.Vertical;
cpe.SuppressPostBack = true;
cpe.TextLabelID = lblHead.ID;
cpe.CollapsedText = c1.name + " " + c1.app_date.ToString().Substring(0, 10) + " " + Convert.ToString(diff.Days);
cpe.ExpandedText = c1.name + " " + c1.app_date.ToString().Substring(0, 10) + " " + Convert.ToString(diff.Days);
PlaceHolder1.Controls.Add(panelHead);
PlaceHolder1.Controls.Add(panelBody);
PlaceHolder1.Controls.Add(cpe);
i++;
}
TblHolder.Controls.Add(tbl);
}
}
}