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.
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.
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.
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.
Next go to the Grid View and select "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.
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.
OK now we come to the Template Field.
Go to Grid View and Select Edit Templates.
Select the Column that we added as "ItemTemplate".
Drag and Drop a "dropdownlist" to "Item Template".
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".
Then Configure as below.
That's it. Make sure you Enable the AutoPostBack of "dropdownlist".
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 Behind "AdministratorPage.aspx" page
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
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
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
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AdministratorPage.aspx.cs"Inherits="AdministratorPage" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
- CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
- GridLines="None" Height="183px" Width="417px" DataKeyNames="UserID"
- >
- <AlternatingRowStyle BackColor="White" />
- <Columns>
- <asp:BoundField DataField="UserName" HeaderText="User Name"
- SortExpression="UserName" />
- <asp:BoundField DataField="FName" HeaderText="Full Name"
- SortExpression="FName" />
- <asp:BoundField DataField="Email" HeaderText="E-mail"
- SortExpression="Email" />
- <asp:BoundField DataField="Status" HeaderText="Status"
- SortExpression="Status" Visible="False" />
- <asp:BoundField DataField="UserID" HeaderText="UserID" InsertVisible="False"
- ReadOnly="True" SortExpression="UserID" Visible="False" />
- <asp:TemplateField HeaderText="Status">
- <ItemTemplate>
- <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
- onselectedindexchanged="OnSelectedIndexChanged"
- SelectedValue='<%# Bind("Status") %>'>
- <asp:ListItem>Enable</asp:ListItem>
- <asp:ListItem>Disable</asp:ListItem>
- </asp:DropDownList>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- <EditRowStyle BackColor="#2461BF" />
- <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
- <RowStyle BackColor="#EFF3FB" />
- <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
- <SortedAscendingCellStyle BackColor="#F5F7FB" />
- <SortedAscendingHeaderStyle BackColor="#6D95E1" />
- <SortedDescendingCellStyle BackColor="#E9EBEF" />
- <SortedDescendingHeaderStyle BackColor="#4870BE" />
- </asp:GridView>
- <asp:SqlDataSource ID="SqlDataSource1" runat="server"
- ConnectionString="<%$ ConnectionStrings:RegAndLoginConnectionString %>"
- DeleteCommand="DELETE FROM [UserData] WHERE [UserID] = @UserID"
- InsertCommand="INSERT INTO [UserData] ([UserName], [FName], [Email], [Status]) VALUES (@UserName, @FName, @Email, @Status)"
- SelectCommand="SELECT [UserName], [FName], [Email], [Status], [UserID] FROM [UserData]"
- UpdateCommand="UPDATE [UserData] SET [UserName] = @UserName, [FName] = @FName, [Email] = @Email, [Status] = @Status WHERE [UserID] = @UserID">
- <DeleteParameters>
- <asp:Parameter Name="UserID" Type="Int64" />
- </DeleteParameters>
- <InsertParameters>
- <asp:Parameter Name="UserName" Type="String" />
- <asp:Parameter Name="FName" Type="String" />
- <asp:Parameter Name="Email" Type="String" />
- <asp:Parameter Name="Status" Type="String" />
- </InsertParameters>
- <UpdateParameters>
- <asp:Parameter Name="UserName" Type="String" />
- <asp:Parameter Name="FName" Type="String" />
- <asp:Parameter Name="Email" Type="String" />
- <asp:Parameter Name="Status" Type="String" />
- <asp:Parameter Name="UserID" Type="Int64" />
- </UpdateParameters>
- </asp:SqlDataSource>
- <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
- <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
- Text="SignOut" />
- </form>
- </body>
- </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 |
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
- public static DataSet UpdateStatus(string UserName,string Status)
- {
- DataSet dataSet = new DataSet();
- using (SqlConnection connection = ConnectionManager.GetDatabaseConnection())
- {
- string sql = "UPDATE UserData SET Status=@Status WHERE UserName=@UserName";
- SqlCommand command = new SqlCommand(sql, connection);
- command.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName;
- command.Parameters.Add("@Status", SqlDbType.VarChar).Value = Status;
- command.CommandType = CommandType.Text;
- SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
- dataAdapter.Fill(dataSet, "UserName");
- }
- return dataSet;
- }
Code Behind "AdministratorPage.aspx" page
Code Snippet - "AdministratorPage.cs"
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class AdministratorPage : System.Web.UI.Page
- {
- protected void OnSelectedIndexChanged(object sender, EventArgs e)
- {
- DropDownList ddl = (DropDownList)sender;
- GridViewRow row = (GridViewRow)ddl.Parent.Parent;
- int idx = row.RowIndex;
- string UserName = (string)row.Cells[0].Text;
- string Status = (string)ddl.Text;
- RegisterUser.UpdateStatus(UserName,Status);
- Label1.Text = UserName+" Account has "+Status;
- GridView1.DataBind();
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- Response.Redirect("LogOutPage.aspx");
- }
- }
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"
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class LogOutPage : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Session.Remove("user");
- Response.Redirect("LoginPage.aspx");
- }
- }
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