FindControl() 不能用变量做参数? 在线等~~~~急。。。。
请问,aspx页有固定5个Panel,如何在.cs页根据一个变量i进行Visible的显示?
如:
<asp:Panel ID="Panel1" runat="server" Visible="false">
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" Visible="false">
</asp:Panel>
<asp:Panel ID="Panel3" runat="server" Visible="false">
</asp:Panel>
<asp:Panel ID="Panel4" runat="server" Visible="false">
</asp:Panel>
<asp:Panel ID="Panel5" runat="server" Visible="false">
</asp:Panel>
-------------------------------------------
.CS
for (int i = 1; i < numSum; i++)
{}
例如i=3,则Panel1~3的Visible = true;该怎么写?
我这样写的:
for (int i = 1; i < numSum; i++)
{
string PanelID = "Panel" + i.ToString();
Panel PanelNow = (Panel)FindControl(PanelID);
PanelNow.Visible = true;
}
但提示无实例化。断点显示,PanelID可以传入,但PanelNow的值为null.
请教该怎么写?
[解决办法]
--页面中form内
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server" Visible="true">a
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" Visible="false">b
</asp:Panel>
<asp:Panel ID="Panel3" runat="server" Visible="false">c
</asp:Panel>
<asp:Panel ID="Panel4" runat="server" Visible="false">d
</asp:Panel>
<asp:Panel ID="Panel5" runat="server" Visible="false">e
</asp:Panel>
</div>
</form>
--cs
protected void Page_Load(object sender, EventArgs e)
{
bindData();
}
protected void bindData()
{
int numSum = 5;
Panel PanelNow = new Panel();
for (int i = 1; i <= numSum; i++)
{
string PanelID = "Panel" + i.ToString();
PanelNow = (Panel)Page.FindControl(PanelID);
PanelNow.Visible = true;
}
}