在html属性中使用Enum值

时间:2021-02-25 19:20:20

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

可以请一些人告诉我为什么这不起作用,因为我输了,有点新的c#和来自php环境

<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%>

2 个解决方案

#1


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/

按照建议,你应该使用.NET MVC而不是webforms,所以你应该使用@html.DropDownList来代替使用 ,在这里了解更多信息:http://agilewarrior.wordpress.com/2012/12 / 13 /如何对简单的HTML dropdownlistfor-MVC-NET / :listbox>

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:

你仍然可以在.NET MVC中使用ASPX作为渲染引擎,在这种情况下你可以使用:

<%: Html.DropDownList(...) %>

#2


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.

我对这种事情有点生疏,@ chris是对的,如果你的C#等开始进入MVC,ASP.Net将在5到10年内死掉。

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();
}

#1


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/

按照建议,你应该使用.NET MVC而不是webforms,所以你应该使用@html.DropDownList来代替使用 ,在这里了解更多信息:http://agilewarrior.wordpress.com/2012/12 / 13 /如何对简单的HTML dropdownlistfor-MVC-NET / :listbox>

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:

你仍然可以在.NET MVC中使用ASPX作为渲染引擎,在这种情况下你可以使用:

<%: Html.DropDownList(...) %>

#2


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.

我对这种事情有点生疏,@ chris是对的,如果你的C#等开始进入MVC,ASP.Net将在5到10年内死掉。

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();
}