tags:

views:

866

answers:

4

ASPX Code

<asp:RadioButtonList ID="rbServer" runat="server" >
   <asp:ListItem Value=<%=ServerDeveloper%>> Developer </asp:ListItemv
   <asp:ListItem Value="dev.ahsvendor.com"> dev.test.com</asp:ListItem>
   <asp:ListItem Value="staging.ahsvendor.com"> staging.test.com</asp:ListItem>
</asp:RadioButtonList>

ASPX.CS - Codebehind

const string ServerDeveloper = "developer";

ASPX Error: Code blocks are not supported in this context.

Question: So what is the correct way to tie an dropdown/radio buttion/... ASPX value to a constant that is shared with the CodeBehind code?

I know that I could do rbServer.Add.Item("developer") [from the CodeBehind], but is there a way to achieve it from the Presentation side of things?

+1  A: 

Would it be:

rbServer.Items.Add(ServerDeveloper)
Ok, so since you want to do it from presentation...It is possible, but horribly ugly:
<div>
<% rbServer.Items.Add(new ListItem("Dev", ServerDeveloper)); %>
<asp:RadioButtonList ID="rbServer" runat="server">
    <asp:ListItem Value="Blah">Blah</asp:ListItem>
</asp:RadioButtonList>
</div>

Note that the code block has to be above the markup - if you put it below, it doesn't seem to work. Note also that the const will have to be protected in order for the page to access it. This feels terribly like a hack to me, but there it is.

Cory Foy
I agree the inline code is ugly. I just expected <%=... to work. Thanks
However, it has the nice "detail" of using the constant in both arenas.
A: 

In retrospect, the better solution would be to add it from the codebehind using rbServer.Items.Add()

Mitchel Sellers
I disagree, if they are constant values its easier to put them in the aspx because when generating resource files, they are automatically generated in the resx. If you add them in the codebehind, you'll have to link the resource entrees manually with GetLocalResoureObject()...
Peter
A: 

In most cases I add the ListItems to the List in the codebehind, not in the markup. I'm guessing that will solve your problem (even though I think we are missing some information here). Create new ListItems and add them to rbServer's Items collection.

Torbjørn
A: 

I generally try to avoid the RadioButtonList control for the very reason that you have posted. Although I haven't come up with an easy to use alternative :(

Josh