2015-12-02 66 views
1

如何使用代碼後面的變量設置下拉列表的值?ASP.NET中DropDownList的設置值

<asp:DropDownList runat="server"      
    <asp:ListItem Text="SomeText" Value='<%: valudeCodeBehind %>'></asp:ListItem> 
</asp:DropDownList> 

,我把計算後面的代碼串,可以說這是valudeCodeBehind ="113";

但我正在逐漸<%: valudeCodeBehind %>這個值,而不是113.因此,如何將它傳遞。

編輯:Value="<%= valudeCodeBehind %>"也不工作。它是不是一個錯字(有人把在評論)..

+1

「<%= valudeCodeBehind%>」 –

+1

我認爲它的<%=變量%> – Bearcat9425

+0

您可能還typo'd 「價值」 - > valudeCodeBehind。那應該是「valueCodeBehind」嗎? –

回答

2

如果您已經使用代碼隱藏計算值爲什麼不使用類似

IdOfDDL.Items.Insert(0, New ListItem("someText", valudeCodeBhind)) 
在你的代碼隱藏

而不<%搞亂% >並保持你的觀點乾淨。

0

或調用這樣的方法......

代碼隱藏

public DataTable GetString() 
{ 
    //... do what you have to do here 
} 

ASPX

<asp:DropDownList ID="ddlString" DataSource='<%# GetString() %>' 
DataTextField="MyString" DataValueField="StringID" runat="server"> 
</asp:DropDownList> 

代碼隱藏方法應該是公開的,從ASPX頁面訪問。

0

這是行不通的。你可能想嘗試表達式構建器。 Here是一篇很好的文章。

0
<asp:DropDownList runat="server"      
    <asp:ListItem Text="SomeText" Value='<% FunctionFromCodeBehind(); %>'> 
    </asp:ListItem> 
</asp:DropDownList> 

而後面的代碼:

private Int32 FunctionFromCodeBehind() 
{ 
    int result = 0; 
    //calculations here 
    return result; 
} 
相關問題