Wednesday, July 11

Databind dropdowns to Enums

Here is how you do it.

   20     private enum ReportTypes : int
   21     {
   22         ByVendor = 1,
   23         ByCategory = 2,
   24         ByType = 3
   25     }
35 protected void Page_Load(object sender, EventArgs e)
   36     {
   37         if (!IsPostBack)
   38         {
   44             ddlReportTypes.DataSource = Enum.GetNames(typeof(ReportTypes));
45 ddlReportTypes.DataBind();
   46         }
   47     }
   48     protected void ddlReportTypes_SelectedIndexChanged(object sender, EventArgs e)
   49     {
   50         ReportTypes rType =
   51             (ReportTypes)Enum.Parse(typeof(ReportTypes), ddlReportTypes.SelectedValue);
   52         switch (rType)
   53         {
   54             case ReportTypes.ByVendor:
   56                 break;
   57             case ReportTypes.ByCategory:                
   60                 break;
   61             case ReportTypes.ByType:
   62                 break;
   63         }
   64     }

Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]