User and Role Management for an ASP.NET AJAX Server-Centric Based Online Shopping Website - Role Management
(Page 2 of 6 )
First, let’s take a quick glimpse at the running-time snapshot for role management, as is shown in Figure 10 below.
Figure 10—the running-time snapshot for role management
/Building_ASP.NET_AJAX_Server-Centric_Shopping_Website(4)_html_6d6dc141.png)
On the left of this simple page there already exist three typical roles: Super Administrator, Supplier, and Common Customer. On the right of the page there are two image-styled buttons with the first pointing to the "EditRole.aspx" page and the second triggering the action to delete the selected role on the left, respectively.
Let’s see the related code when the page is loaded, as follows:
protected void Page_Load(object sender, EventArgs e) {
//bind data to the control
if(!Page.IsPostBack) {
BindRoleData();
}
///add the dialog that confirms the user to make the deletion
deleteBtn.Attributes.Add("onclick","return confirm('Are you sure to delete the selected items?');");
}
private void BindRoleData(){
///define the class that gets the data
Role role = new Role();
SqlDataReader recr = role.GetRoles();
///Set the control's data source
RoleList.DataSource = recr;
RoleList.DataTextField = "RoleName";
RoleList.DataValueField = "RoleID";
///bind data to the control
RoleList.DataBind();
///Close the database connection
recr.Close();
}
As the above comments indicate, we first bind the corresponding data to the "RoleList" ListBox control by calling a helper function named BindRoleData. Then we attach a client-side click handler to the "deleteBtn" button by invoking the Addmethod of its collection member —Attributes. When the user selects one of the roles on the left side of the page, the deletion-operation-related event handler will be triggered. The following lists the associated code snippet:
protected void deleteBtn_Click(object sender,ImageClickEventArgs e){
if(RoleList.SelectedIndex <= -1) {
Response.Write("<script>window.alert('Please select the item.')</script>");
return;
}
///define the classRole
Role role = new Role();
///delete data
role.DeleteRole(Int32.Parse(RoleList.SelectedValue));
///display the info of the operating result
Response.Write("<script>window.alert('Deleting successfully!')</script>");
///rebind data
BindRoleData();
}
Since the code is well commented, we won't discuss it much. The only point that needs to be noticed is that after the deletion is finished the "RoleList" ListBox is updated.
Next: New Role >>
More ASP.NET Articles
More By Xianzhong Zhu