Thursday, February 07, 2008

asp.net 2.0 Roles DropDownList Control in C#

Translation of this post to french at :
http://s4n44.blogspot.com/2008/02/aspnet-20-dropdownlist-de-rles-enc.html

This is a user control, which encapsulates a DropDownList, an ObjectDataSource and custom, simplified roles class. The user control will connect automatically (of course), with the aspnetdb database pointed to in the web.config
for vb.net users, here's a nice converter provided by Telerik (http://www.telerik.com ) :
http://www.codechanger.com/

Here's the code for the user control :

<%@ Control Language="C#" AutoEventWireup="true" CodeFile=" ddlRoles.ascx.cs" Inherits="RoleControl" %>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="sourceRoles" DataTextField="RoleName"
DataValueField="RoleName" Width="185px">
</asp:DropDownList>
<asp:ObjectDataSource ID="sourceRoles" runat="server" SelectMethod="GetRoles"
TypeName="CustomRoles"></asp:ObjectDataSource>


Code behind for the user control:


using System;

public partial class RoleControl : System.Web.UI.UserControl
{
public String SelectedValue
{
get { return this.DropDownList1.SelectedValue; }
set { this.DropDownList1.SelectedValue = value; }
}
}


The object datasource uses the class below :


using System;
using System.Web.Security;
using System.Collections.Generic;

public class CustomRoles
{
private string mRoleName;

public string RoleName
{
get { return mRoleName; }
set { mRoleName = value; }
}

/// <summary>
/// this function, loops through all roles in the database, and builds
/// a collection of CustomRoles instances into a generic list of CustomRoles
/// then we return the list as the source for the dropdown.
/// The RoleName property is used to bind the datavaluefield and datatextfield
/// in the dropdownlist
/// </summary>
/// <returns></returns>
public List<CustomRoles> GetRoles()
{
//getroles
string[] roleslist = Roles.GetAllRoles();

int i = 0;
//instance of customroles
CustomRoles br;

//create the list (or collection of CustomRoles instances)
List<CustomRoles> lstbr = new List<CustomRoles>();

//fill the list by looping through the array of strings
foreach (string brole in roleslist)
{
br = new CustomRoles();
br.RoleName = brole;
lstbr.Add(br);
}

return lstbr;

}

}


Comments are welcome !

3 comments:

gfutfy said...

Youth is not wow gold a time of life;world of warcraft gold it is a state of mind; cheap wow gold it is not a Maple Story Accounts matter of rosy cheeks, red lips and supple knees;mesos it is a matter of the will, a quality of the imagination,wow gold kaufen a vigor of the emotions; it is the freshness wow geld of the deep springs of life.maple story mesos Youth means a tempera-mental predominance of courage over timidity, of the appetite for adventure over the love of ease. This often exists in a man of 60 more than a boy of 20.wow gold farmen Nobody grows old merely by a number of years.maple story money We grow old by deserting our ideals.ms mesos Years may wrinkle the skin, but to give up enthusiasm wrinkles the soul. Worry, fear, self-distrust bows the heart and turns the spring back to dust. Whether 60 or 16, there is in every human being’wow powerleveling s heart the lure of wonder, the unfailing childlike appetite of what’s maple story money next and the joy of the game of living.powerlevel In the center of your heart and my heart there is a wireless station: so long as it receives messages maplestory powerleveling of beauty, hope, cheer,world of warcraft power leveling courage and power from men and from the Infinite

RRave said...

Dear Sir,

I have a launched new web site for .NET programming resources. www.codegain.com. I would like to invite to the codegain.com as author and supporter. I hope you will joins with us soon.

Thank You
RRaveen
Founder www.codegain.com

r4i said...

THANKS BUDDY FOR SHARING CODE. and the website is really awesome.......