2008-10-14 26 views
4

ASPX代碼C#(ASP.Net)鏈接的選擇值來常數代碼隱藏

 
<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 - 代碼隱藏

 
const string ServerDeveloper = "developer"; 

ASPX錯誤:代碼塊不是在這方面得到支持。

問:那麼,什麼是綁一個下拉/收音機buttion正確的方法/ ... ASPX值與該代碼隱藏代碼共享一個常數?

我知道我可以做rbServer.Add.Item(「開發人員」)[來自CodeBehind],但有沒有辦法從事物的演示方面實現它?

+0

我們在這裏缺少一些信息嗎?請修正ASPX代碼! – 2008-10-14 16:15:10

回答

3

難道是:

rbServer.Items.Add(ServerDeveloper)

好了,既然你想從演示做...這是可能的,但可怕的醜陋:

 
<div> 
<% rbServer.Items.Add(new ListItem("Dev", ServerDeveloper)); %> 
<asp:RadioButtonList ID="rbServer" runat="server"> 
    <asp:ListItem Value="Blah">Blah</asp:ListItem> 
</asp:RadioButtonList> 
</div> 

注意,代碼塊有以上的標記 - 如果你把它放在下面,它似乎不起作用。還要注意const必須被保護才能讓頁面訪問它。對我來說,這感覺非常像黑客,但它確實是。

+0

我同意內嵌代碼是醜陋的。我只是希望<%= ...到工作。 感謝 – 2008-10-14 20:28:00

+0

然而,在這兩個領域使用常量的漂亮「細節」。 – 2008-10-14 20:28:56

0

回想起來,更好的解決方案是將其使用rbServer.Items.Add()的代碼隱藏

0

在大多數情況下,我的listItems中添加到列表中的代碼隱藏,而不是在標記添加。我猜這會解決你的問題(儘管我認爲我們在這裏錯過了一些信息)。創建新的ListItems並將它們添加到rbServer的Items集合中。

0

我一般儘量避免爲您發佈的真正原因RadioButtonList控件。雖然我還沒有想出了一個易於使用的替代:(