今天学习了在用户注册界面中利用工具箱里的GridView工具来显示用户注册与所要添加角色的名称信息以表格的形式来显示在GridView中。要实现此功能则需要从数据库中调用所要显示的字段。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
public partial class zhuce : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string[] roles = Roles.GetAllRoles();
foreach (string str in roles)
{
this.ListBox1.Items.Add(str);
}
}
//MembershipUser use = Membership.GetAllUsers();
DataTable dt = new DataTable();
DataColumn dc = new DataColumn();
dc.DataType=typeof(string);
dc.ColumnName = "name";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType=typeof(string);
dc.ColumnName = "email";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "roles";
dc.DataType=typeof(string);
dt.Columns.Add(dc);
foreach(MembershipUser item in Membership.GetAllUsers())
{
DataRow row = dt.NewRow();
row["name"] = item.UserName;
row["email"] = item.Email;
string[] roes = Roles.GetRolesForUser(item.UserName);
if (roes.Length > 0)
{
row["roles"] = roes[0];
}
dt.Rows.Add(row);
}
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Roles.CreateRole(this.TextBox1.Text);
this.ListBox1.Items.Add(this.TextBox1.Text);
}
protected void Button2_Click(object sender, EventArgs e)
{
this.Label1.Text = this.TextBox1.Text;
}
}