我可以在findcontrol中使用数组吗?

你好,我有一个有四个标签的DataList ,它们都以1-4号结尾。 在我的代码后面我有一个for循环和一个数组,我想设置Datalist的标签。

 for (int x = 0; x< cartreciept.Items.Count; x++) { DataListItem item = cartreceipt.Items[x]; string catalogtype = ("select CatalogType From SC Where OrNum=" + OrNum) if (catalogtype="TC") { ((Panel)item.FindControl("pnlIprintInfo")).Visible = true; string scRID = ("Select SCRID From SC Where OrNum =" + OrNum for(int y = 1; y<5; y++) { string lT[y] = (Select LineText From table Where SCartRD =" + scRID + " AND LN=" + y) ((Label)item.FindControl("lbl[y]")).text = lT[y]; } } } 

那么((Label)item.FindControl("lbl[y]"))起作用吗? 大部分代码只是伪代码,直到我详细说明。 如果您需要了解其他任何我可以提供需要知道的内容,我也愿意接受其他建议。 感谢任何能提供帮助的人。

您可以使用此代码 – 基于string.Format("....{0}",arguments)

 var control = (Label)item.FindControl(string.Format("lbl{0}",y)); 

我建议你使用DataList.ItemDataBound

链接: http : //msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx