Reputation: 1096
How to set value of drop down list, using variable from code behind?
<asp:DropDownList runat="server"
<asp:ListItem Text="SomeText" Value='<%: valudeCodeBehind %>'></asp:ListItem>
</asp:DropDownList>
and I put calculate string in code behind, lets say it is valudeCodeBehind ="113";
But I am getting <%: valudeCodeBehind %>
this value instead of 113. So how to pass it.
edit: Value="<%= valudeCodeBehind %>"
doesnt work either. And it is not a typo (someone put that in comments)..
Upvotes: 2
Views: 1201
Reputation: 1656
<asp:DropDownList runat="server"
<asp:ListItem Text="SomeText" Value='<% FunctionFromCodeBehind(); %>'>
</asp:ListItem>
</asp:DropDownList>
And code behind:
private Int32 FunctionFromCodeBehind()
{
int result = 0;
//calculations here
return result;
}
Upvotes: 0
Reputation: 508
That won't work. You may want to try expression builder. Here is a good article about it.
Upvotes: 0
Reputation: 1049
Or calling the method like this...
CodeBehind
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>
The Codebehind method should be public to access from ASPX page.
Upvotes: 0
Reputation: 4446
if you're already using codebehind to calculate values why not use something like
IdOfDDL.Items.Insert(0, New ListItem("someText", valudeCodeBhind))
in your codebehind without messing with <% %> and keep your view clean.
Upvotes: 3