1

can some one please tell me why this does not work, because im lost, kind of new to c# and coming from php environment

<asp:ListItem Value="<%:(int)Student.Classes.Enum.Enum.gender.male%>">Male</asp:ListItem>

but if i use this exact code outside it works perfect e.g

I am <%:(int)Student.Classes.Enum.Enum.gender.male%>
6
  • What do you expect to see and what are you seeing? Commented Apr 2, 2013 at 16:10
  • i expect to see 1, when i use it alone like the second line of code, i get I am 1, which is right, but in the first, that whole line is printed as is, like its text Commented Apr 2, 2013 at 16:12
  • That's what you did. You are giving the list item value a 1, but the text is Male. Did you look at the HTML source to see what it looks like? Commented Apr 2, 2013 at 16:14
  • I see you are using classic ASP.NET Web Forms -- I wouldn't recommend this for new development. Use Razor engine "Web Pages" instead. It's a much simpler and more modern approach that works with HTML instead of against it. See asp.net/web-pages Commented Apr 2, 2013 at 16:16
  • this is whats in the html **<option value="&lt;%:(int)Student.Classes.Enum.Enum.gender.male%>">Male</option> ** Commented Apr 2, 2013 at 16:17

2 Answers 2

1

As was recommended, you should use .NET MVC instead of webforms, so instead of using <asp:ListBox>, you should use @Html.DropDownList Learn more about it here: http://agilewarrior.wordpress.com/2012/12/13/how-to-simple-html-dropdownlistfor-mvc-net/

Your code for the dropdown/listbox would be:

@Html.DropDownList("StudentGender",
    new List<SelectListItem>() {  
        new SelectListItem(){ Text="Male",Value=Student.Classes.Enum.Enum.gender.male.ToString() },
        new SelectListItem(){ Text="Female",Value=Student.Classes.Enum.Enum.gender.female.ToString() }
    }, new { @class = "input-medium", size="6", multiple="multiple" } )

You can still use ASPX for the render engine in .NET MVC, which in that case you would use:

<%: Html.DropDownList(...) %>
Sign up to request clarification or add additional context in comments.

Comments

0

I'm a little rusty with this kind of thing, @chris is right, get into MVC if your starting on C#, etc. ASP.Net will be dead in 5-10 years.

anyway, I'm presuming you've got something like this:

<asp:ListBox runat="server" Id="listBox">
</asp:ListBox>

you prob want to set this in code:

public Page_Load()
{
    listBox.DataSource = Enum.GetValues(typeof(Student.Classes.Enum.Enum.gender));
    listBox.DataBind();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.