Can use variable as server control Id?

S

sdfarmer

I need to Name my menu Controls using a string Array like this:
---------------------------------------------------------------
<script language="C#" runat="server">
public String MenuCtrlIds[]=new
String[]{"FileMenuCtrl","PlayMenuCtrl","HomeMenuCtrl","UserMenuCtrl","PersonalPreferenceCtrl"};
</script>
<asp:HyperLink Id=MenuCtrlIds[0]
NavigateUrl="javascript:location.href=location.href" Title="Refresh"
Text="File" runat=Server />
<asp:HyperLink Id=MenuCtrlIds[1] NavigateUrl="xxx" Text="Back to Home"
runat=Server />
<asp:HyperLink Id=MenuCtrlIds[2] NavigateUrl="xxx" Text="User"
runat=Server />
....

but I got this error:
Parser Error Message: 'MenuCtrlIds[0]' is not a valid identifier.
Why?Thx~
 
C

Craig Deelsnyder

I need to Name my menu Controls using a string Array like this:
---------------------------------------------------------------
<script language="C#" runat="server">
public String MenuCtrlIds[]=new
String[]{"FileMenuCtrl","PlayMenuCtrl","HomeMenuCtrl","UserMenuCtrl","PersonalPreferenceCtrl"};
</script>
<asp:HyperLink Id=MenuCtrlIds[0]
NavigateUrl="javascript:location.href=location.href" Title="Refresh"
Text="File" runat=Server />
<asp:HyperLink Id=MenuCtrlIds[1] NavigateUrl="xxx" Text="Back to Home"
runat=Server />
<asp:HyperLink Id=MenuCtrlIds[2] NavigateUrl="xxx" Text="User"
runat=Server />
...

but I got this error:
Parser Error Message: 'MenuCtrlIds[0]' is not a valid identifier.
Why?Thx~

I believe you can, but you can't do it this way; the ID attribute is read
literally as a string; it won't interpret it. You'd have to do this via
the code-behind (er, server code in Page_Load, e.g.). Just set the IDs to
something in the front-end, then reset them in the code-behind, in
Page_Load, e.g.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top