Wednesday

C# bind dropdown to enum

For example we have enumeration Enum1 and want to bind ddEnum1.
here is how to do it in directly into html page , when GetEnum1 is function in codebehing , that will be getting SelectedValue for every record object (passign by parameter)
     
<asp:TemplateField HeaderText="Type" ItemStyle-Width="5%" ItemStyle-Wrap=False HeaderStyle-HorizontalAlign=Center HeaderStyle-Wrap=False ItemStyle-HorizontalAlign=Center>
<ItemTemplate>
<asp:DropDownList id="typeDropDown" runat="server" Width='150'
DataSource="<%# Enum.GetNames(typeof(myEnum1)) %>"
SelectedValue='<%# GetEnum1(Container.DataItem) %>'
OnInit="InitDropDown" />

</ItemTemplate>
</asp:TemplateField>


here is how to do it in codebehind
       
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
ddEnum1.DataSource = Enum.GetNames(typeof(Enum1));
ddEnum1.DataBind();
}
}

private void ddEnum1_SelectedIndexChanged(object sender, System.EventArgs e)
{
Enum1 selectedEnum1 = (Enum1)Enum.Parse(ddEnum1.SelectedValue);
}



No comments:

test smtp server with powershell

Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...