. To implement, the "Enable" button, make the following changes to the gridview control.
First Change: Specify the CommandArgument attribute of the Button control in the Template column.
<asp:TemplateField HeaderText="Enable">
<ItemTemplate>
<asp:Button ID="btnEnable" runat="server" CommandArgument='<%# Eval("UserName") %>'
Text="Enable" Enabled='<%#Convert.ToInt32(Eval("HoursElapsed")) > 24%>'/>
</ItemTemplate>
</asp:TemplateField>
Second Change: Generate the "RowCommand" event handler for the GridView control.
1. Right Click on the GridView Control and Select properties.
2. In the "Properties Window", click on events icon.
3. In the events windows, double click on the text box next to "Row Command" event.
With these 2 changes the HTML of the "LockedAccounts.aspx" should look as shown below.
<div style="font-family: Arial">
<asp:GridView ID="gvLockedAccounts" runat="server" AutoGenerateColumns="False"
OnRowCommand="gvLockedAccounts_RowCommand">
<Columns>
<asp:BoundField DataField="UserName" HeaderText="User Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField DataField="LockedDateTime" HeaderText="Locked Date & Time" />
<asp:BoundField DataField="HoursElapsed" HeaderText="Hours Elapsed">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Enable">
<ItemTemplate>
<asp:Button ID="btnEnable" CommandArgument='<%# Eval("UserName") %>' runat="server"
Text="Enable" Enabled='<%#Convert.ToInt32(Eval("HoursElapsed")) > 24%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
Copy and paste the following private method in "LockedAccounts.aspx.cs" page.
private void EnableUserAccount(string UserName)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spEnableUserAccount", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramUserName = new SqlParameter()
{
ParameterName = "@UserName",
Value = UserName
};
cmd.Parameters.Add(paramUserName);
con.Open();
cmd.ExecuteNonQuery();
}
}
Invoke EnableUserAccount() method, in RowCommand() event handler as shown below.
protected void gvLockedAccounts_RowCommand(object sender,GridViewCommandEventArgs e)
{
EnableUserAccount(e.CommandArgument.ToString());
GetData();
}
First Change: Specify the CommandArgument attribute of the Button control in the Template column.
<asp:TemplateField HeaderText="Enable">
<ItemTemplate>
<asp:Button ID="btnEnable" runat="server" CommandArgument='<%# Eval("UserName") %>'
Text="Enable" Enabled='<%#Convert.ToInt32(Eval("HoursElapsed")) > 24%>'/>
</ItemTemplate>
</asp:TemplateField>
Second Change: Generate the "RowCommand" event handler for the GridView control.
1. Right Click on the GridView Control and Select properties.
2. In the "Properties Window", click on events icon.
3. In the events windows, double click on the text box next to "Row Command" event.
With these 2 changes the HTML of the "LockedAccounts.aspx" should look as shown below.
<div style="font-family: Arial">
<asp:GridView ID="gvLockedAccounts" runat="server" AutoGenerateColumns="False"
OnRowCommand="gvLockedAccounts_RowCommand">
<Columns>
<asp:BoundField DataField="UserName" HeaderText="User Name" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField DataField="LockedDateTime" HeaderText="Locked Date & Time" />
<asp:BoundField DataField="HoursElapsed" HeaderText="Hours Elapsed">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Enable">
<ItemTemplate>
<asp:Button ID="btnEnable" CommandArgument='<%# Eval("UserName") %>' runat="server"
Text="Enable" Enabled='<%#Convert.ToInt32(Eval("HoursElapsed")) > 24%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
Copy and paste the following private method in "LockedAccounts.aspx.cs" page.
private void EnableUserAccount(string UserName)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spEnableUserAccount", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter paramUserName = new SqlParameter()
{
ParameterName = "@UserName",
Value = UserName
};
cmd.Parameters.Add(paramUserName);
con.Open();
cmd.ExecuteNonQuery();
}
}
Invoke EnableUserAccount() method, in RowCommand() event handler as shown below.
protected void gvLockedAccounts_RowCommand(object sender,GridViewCommandEventArgs e)
{
EnableUserAccount(e.CommandArgument.ToString());
GetData();
}