Tuesday 2 April 2013

Administrator Control and Features


Administrator Control and Features


We will look at how the Administrator will view the submitted data and how he/she will enable the account. I have use a Grid view to view all the details of the users.

Viewing the Data using a grid view is very simple.

So what I am going to do different here is insert a Drop Down List inside the Grid View and administrator will be allow to Enable or Disable the User Status by the Drop Down List.

This is the Administrator view will look like. It is a Simple administration form and there is a sign out button to log out from the page.

Administrator View

Source code for the Form. But you can follow my below instructions which will create the Grid View using the "Design View". Try it out if any problem come to the source code.

Code Snippet - AdministratorPage.aspx.cs
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AdministratorPage.aspx.cs"Inherits="AdministratorPage" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title></title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     
  12.  
  13.     
  14.     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
  15.         CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
  16.         GridLines="None" Height="183px" Width="417px" DataKeyNames="UserID"
  17.         >
  18.         <AlternatingRowStyle BackColor="White" />
  19.         <Columns>
  20.             <asp:BoundField DataField="UserName" HeaderText="User Name"
  21.                 SortExpression="UserName" />
  22.             <asp:BoundField DataField="FName" HeaderText="Full Name"
  23.                 SortExpression="FName" />
  24.             <asp:BoundField DataField="Email" HeaderText="E-mail"
  25.                 SortExpression="Email" />
  26.             <asp:BoundField DataField="Status" HeaderText="Status"
  27.                 SortExpression="Status" Visible="False" />
  28.             <asp:BoundField DataField="UserID" HeaderText="UserID" InsertVisible="False"
  29.                 ReadOnly="True" SortExpression="UserID" Visible="False" />
  30.             <asp:TemplateField HeaderText="Status">
  31.                 <ItemTemplate>
  32.                     <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
  33.                         onselectedindexchanged="OnSelectedIndexChanged"
  34.                         SelectedValue='<%# Bind("Status") %>'>
  35.                         <asp:ListItem>Enable</asp:ListItem>
  36.                         <asp:ListItem>Disable</asp:ListItem>
  37.                     </asp:DropDownList>
  38.                 </ItemTemplate>
  39.             </asp:TemplateField>
  40.         </Columns>
  41.         <EditRowStyle BackColor="#2461BF" />
  42.         <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  43.         <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  44.         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  45.         <RowStyle BackColor="#EFF3FB" />
  46.         <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  47.         <SortedAscendingCellStyle BackColor="#F5F7FB" />
  48.         <SortedAscendingHeaderStyle BackColor="#6D95E1" />
  49.         <SortedDescendingCellStyle BackColor="#E9EBEF" />
  50.         <SortedDescendingHeaderStyle BackColor="#4870BE" />
  51.     </asp:GridView>
  52.     <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  53.         ConnectionString="<%$ ConnectionStrings:RegAndLoginConnectionString %>"
  54.         DeleteCommand="DELETE FROM [UserData] WHERE [UserID] = @UserID"
  55.         InsertCommand="INSERT INTO [UserData] ([UserName], [FName], [Email], [Status]) VALUES (@UserName, @FName, @Email, @Status)"
  56.         SelectCommand="SELECT [UserName], [FName], [Email], [Status], [UserID] FROM [UserData]"
  57.         UpdateCommand="UPDATE [UserData] SET [UserName] = @UserName, [FName] = @FName, [Email] = @Email, [Status] = @Status WHERE [UserID] = @UserID">
  58.         <DeleteParameters>
  59.             <asp:Parameter Name="UserID" Type="Int64" />
  60.         </DeleteParameters>
  61.         <InsertParameters>
  62.             <asp:Parameter Name="UserName" Type="String" />
  63.             <asp:Parameter Name="FName" Type="String" />
  64.             <asp:Parameter Name="Email" Type="String" />
  65.             <asp:Parameter Name="Status" Type="String" />
  66.         </InsertParameters>
  67.         <UpdateParameters>
  68.             <asp:Parameter Name="UserName" Type="String" />
  69.             <asp:Parameter Name="FName" Type="String" />
  70.             <asp:Parameter Name="Email" Type="String" />
  71.             <asp:Parameter Name="Status" Type="String" />
  72.             <asp:Parameter Name="UserID" Type="Int64" />
  73.         </UpdateParameters>
  74.     </asp:SqlDataSource>
  75.     <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
  76.     
  77.  
  78.     
  79.     <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
  80.         Text="SignOut" />
  81.     
  82.  
  83.     
  84.     </form>
  85. </body>
  86. </html>

Now here are the steps in Design View.

I am more concentrating on how to do the status "dropdownlist". I have explained deeply about how to add gridview in this post.

But I have given the steps as possible.

First add a grid view from Data ToolBox.

Then configure the data source.

Configure Data Source


Configure Data Source

Select the columns that you need to view. You don't have to select the password column.

Also there is a option in grid view where you can disable the view of the columns so we can avoid unnecessary columns.

For example we don't need the "userID" column. So we can disable this column in Grid view.

So why I am selecting "UserID" here is it is the primary key. So to enable the Delete and Update option in Grid view we need the Primary key column.

Select the Columns

Test the Query
Next go to the Grid View and select "Edit Columns".

Edit Columns

You can see all the columns here. We will add another Template Field which will be used to create the status drop down list. Name it as Status.

Also we will be removing the unnecessary columns.



You Can Set the Visible property of the Unnecessary columns to "False". For instance "Status" and "UserID" columns.

Because we are going to add additional "Status" Template Field and "UserID" is system generated so we don't need it for viewing purpose.

"Status" property


OK now we come to the Template Field.

Go to Grid View and Select Edit Templates.

Edit Templates

Select the Column that we added as "ItemTemplate".

Select Column

Drag and Drop a "dropdownlist" to "Item Template".

Edit Template
Add List Item

Add List Items "Enable" and "Disable" to the "dropdownlist".

Now we have given the Client side value.

Next is the "dropdownlist" should display the database table values.

So we will bind the "dropdownlist" to the "Status" Column in the table.

Go to "Edit DataBindings" of "dropdownlist".

Edit DataBindings

Then Configure as below.

Bind to Status Column


That's it. Make sure you Enable the AutoPostBack of "dropdownlist".

Enable AutoPostBack

That is all about the interface side. But still we did not add methods that update the table when the status is changed in "dropdownlist".

So to change the status I add a new method inside the "RegisterUser.cs" Class.

Code Snippet - "UpdateStatus" Method
  1. public static DataSet UpdateStatus(string UserName,string Status)
  2.     {
  3.         DataSet dataSet = new DataSet();
  4.  
  5.         using (SqlConnection connection = ConnectionManager.GetDatabaseConnection())
  6.         {
  7.  
  8.             string sql = "UPDATE UserData SET Status=@Status WHERE UserName=@UserName";
  9.             SqlCommand command = new SqlCommand(sql, connection);
  10.             command.Parameters.Add("@UserName"SqlDbType.VarChar).Value = UserName;
  11.             command.Parameters.Add("@Status"SqlDbType.VarChar).Value = Status;
  12.             command.CommandType = CommandType.Text;
  13.  
  14.             SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
  15.  
  16.             dataAdapter.Fill(dataSet, "UserName");
  17.         }
  18.  
  19.         return dataSet;
  20.     }


Code Behind "AdministratorPage.aspx" page


Code Snippet - "AdministratorPage.cs"
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. public partial class AdministratorPage : System.Web.UI.Page
  9. {
  10.     protected void OnSelectedIndexChanged(object sender, EventArgs e)
  11.     {
  12.  
  13.         DropDownList ddl = (DropDownList)sender;
  14.         GridViewRow row = (GridViewRow)ddl.Parent.Parent;
  15.  
  16.         int idx = row.RowIndex;
  17.         string UserName = (string)row.Cells[0].Text;
  18.         string Status = (string)ddl.Text;
  19.  
  20.         RegisterUser.UpdateStatus(UserName,Status);
  21.  
  22.         Label1.Text = UserName+" Account has "+Status;
  23.  
  24.         GridView1.DataBind();
  25.  
  26.             
  27.     }
  28.  
  29.     protected void Button1_Click(object sender, EventArgs e)
  30.     {
  31.         Response.Redirect("LogOutPage.aspx");
  32.     }
  33.  
  34. }


Please ignore the "Button1_Click" Method in this tutorial. That is for sign out button. It will redirect to the "LogOutPage.aspx" where the session will be deleted and redirected to "LoginPage". You can send an Email to the user when the account is Enabled.


Go to below and implement inside your "OnSelectedIndexChanged" method.

Sending E- Mails in ASP.NET - Acknowledgement  E-mail
Sending E- Mails in ASP.NET - Feedback E-mail


Code Snippet - "LogOutPage.aspx.cs"
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. public partial class LogOutPage : System.Web.UI.Page
  9. {
  10.     protected void Page_Load(object sender, EventArgs e)
  11.     {
  12.         Session.Remove("user");
  13.         Response.Redirect("LoginPage.aspx");
  14.  
  15.     }
  16. }

This is it, done with the Administrator View. Next is to create a sample Login Page which will redirect the users to the proper page.

Example - Administrator will be directed to this page and other users will redirected to a sample user page.

Demonstration



No comments:

Post a Comment