2015-12-02 73 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; 
}