Azm Amn

Azm Amn

  • NA
  • 108
  • 18k

Onclick Button hide row forever in GridView ASP.NET

Feb 23 2017 6:24 AM

I want to hide/delete a specific row from gridview when user clicks a button. The gridview should not display the hidden/deleted row even if the page is refreshed or loaded for the second time. The data deleted on the gridview should not affect the database.

My Aspx Code:

<asp:Panel ID="Panel1" CssClass="frontimage" runat="server" HorizontalAlign="Center" Height="485px" Width="100%" >
<asp:table runat="server" HorizontalAlign="Center" Width="100%">
<asp:TableRow>
<asp:TableCell>
<h1>Approve or Reject Subjects</h1>
</asp:TableCell>
</asp:TableRow>
</asp:table>
<asp:Table runat="server">
<asp:TableRow>
<asp:TableCell>
Course:
</asp:TableCell>
<asp:TableCell><asp:DropDownList ID="DDL1" runat="server"></asp:DropDownList></asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Registration_ID" DataSourceID="SqlDataSource1" AutoGenerateEditButton="true" PageSize="50" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Approve"
CommandName="Select"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Registration_ID" SortExpression="Registration_ID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Registration_ID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Registration_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student_ID" SortExpression="Student_ID">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Student_ID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Student_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Semester_ID" SortExpression="Semester_ID">
<EditItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Semester_ID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Semester_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course" SortExpression="Course">
<EditItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Course") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Course") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject1" SortExpression="Subject1">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="SubjectName" DataValueField="SubjectName" SelectedValue='<%# Bind("Subject1") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Subject1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject2" SortExpression="Subject2">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="SubjectName" DataValueField="SubjectName" SelectedValue='<%# Bind("Subject2") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("Subject2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject3" SortExpression="Subject3">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource2" DataTextField="SubjectName" DataValueField="SubjectName" SelectedValue='<%# Bind("Subject3") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Subject3") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject4" SortExpression="Subject4">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="SqlDataSource2" DataTextField="SubjectName" DataValueField="SubjectName" SelectedValue='<%# Bind("Subject4") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("Subject4") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject5" SortExpression="Subject5">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="SqlDataSource2" DataTextField="SubjectName" DataValueField="SubjectName" SelectedValue='<%# Bind("Subject5") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("Subject5") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject6" SortExpression="Subject6">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="SqlDataSource2" DataTextField="SubjectName" DataValueField="SubjectName" SelectedValue='<%# Bind("Subject6") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("Subject6") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subject7" SortExpression="Subject7">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList7" runat="server" SelectedValue='<%# Bind("Subject7") %>' DataSourceID="SqlDataSource2" DataTextField="SubjectName" DataValueField="SubjectName">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("Subject7") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TotalCreditHours" SortExpression="TotalCreditHours">
<EditItemTemplate>
<asp:TextBox ID="TextBox11" runat="server" Text='<%# Bind("TotalCreditHours") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("TotalCreditHours") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Subject Registration SystemConnectionString %>" SelectCommand="SELECT [Registration_ID], [Student_ID], [Semester_ID], [Course], [Subject1], [Subject2], [Subject3], [Subject4], [Subject5], [Subject6], [Subject7], [TotalCreditHours] FROM [Registration]" ConflictDetection="CompareAllValues" DeleteCommand="DELETE FROM [Registration] WHERE [Registration_ID] = @original_Registration_ID AND [Student_ID] = @original_Student_ID AND [Semester_ID] = @original_Semester_ID AND [Course] = @original_Course AND (([Subject1] = @original_Subject1) OR ([Subject1] IS NULL AND @original_Subject1 IS NULL)) AND (([Subject2] = @original_Subject2) OR ([Subject2] IS NULL AND @original_Subject2 IS NULL)) AND (([Subject3] = @original_Subject3) OR ([Subject3] IS NULL AND @original_Subject3 IS NULL)) AND (([Subject4] = @original_Subject4) OR ([Subject4] IS NULL AND @original_Subject4 IS NULL)) AND (([Subject5] = @original_Subject5) OR ([Subject5] IS NULL AND @original_Subject5 IS NULL)) AND (([Subject6] = @original_Subject6) OR ([Subject6] IS NULL AND @original_Subject6 IS NULL)) AND (([Subject7] = @original_Subject7) OR ([Subject7] IS NULL AND @original_Subject7 IS NULL)) AND [TotalCreditHours] = @original_TotalCreditHours" InsertCommand="INSERT INTO [Registration] ([Registration_ID], [Student_ID], [Semester_ID], [Course], [Subject1], [Subject2], [Subject3], [Subject4], [Subject5], [Subject6], [Subject7], [TotalCreditHours]) VALUES (@Registration_ID, @Student_ID, @Semester_ID, @Course, @Subject1, @Subject2, @Subject3, @Subject4, @Subject5, @Subject6, @Subject7, @TotalCreditHours)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [Registration] SET [Student_ID] = @Student_ID, [Semester_ID] = @Semester_ID, [Course] = @Course, [Subject1] = @Subject1, [Subject2] = @Subject2, [Subject3] = @Subject3, [Subject4] = @Subject4, [Subject5] = @Subject5, [Subject6] = @Subject6, [Subject7] = @Subject7, [TotalCreditHours] = @TotalCreditHours WHERE [Registration_ID] = @original_Registration_ID AND [Student_ID] = @original_Student_ID AND [Semester_ID] = @original_Semester_ID AND [Course] = @original_Course AND (([Subject1] = @original_Subject1) OR ([Subject1] IS NULL AND @original_Subject1 IS NULL)) AND (([Subject2] = @original_Subject2) OR ([Subject2] IS NULL AND @original_Subject2 IS NULL)) AND (([Subject3] = @original_Subject3) OR ([Subject3] IS NULL AND @original_Subject3 IS NULL)) AND (([Subject4] = @original_Subject4) OR ([Subject4] IS NULL AND @original_Subject4 IS NULL)) AND (([Subject5] = @original_Subject5) OR ([Subject5] IS NULL AND @original_Subject5 IS NULL)) AND (([Subject6] = @original_Subject6) OR ([Subject6] IS NULL AND @original_Subject6 IS NULL)) AND (([Subject7] = @original_Subject7) OR ([Subject7] IS NULL AND @original_Subject7 IS NULL)) AND [TotalCreditHours] = @original_TotalCreditHours">
<DeleteParameters>
<asp:Parameter Name="original_Registration_ID" Type="String" />
<asp:Parameter Name="original_Student_ID" Type="String" />
<asp:Parameter Name="original_Semester_ID" Type="String" />
<asp:Parameter Name="original_Course" Type="String" />
<asp:Parameter Name="original_Subject1" Type="String" />
<asp:Parameter Name="original_Subject2" Type="String" />
<asp:Parameter Name="original_Subject3" Type="String" />
<asp:Parameter Name="original_Subject4" Type="String" />
<asp:Parameter Name="original_Subject5" Type="String" />
<asp:Parameter Name="original_Subject6" Type="String" />
<asp:Parameter Name="original_Subject7" Type="String" />
<asp:Parameter Name="original_TotalCreditHours" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Registration_ID" Type="String" />
<asp:Parameter Name="Student_ID" Type="String" />
<asp:Parameter Name="Semester_ID" Type="String" />
<asp:Parameter Name="Course" Type="String" />
<asp:Parameter Name="Subject1" Type="String" />
<asp:Parameter Name="Subject2" Type="String" />
<asp:Parameter Name="Subject3" Type="String" />
<asp:Parameter Name="Subject4" Type="String" />
<asp:Parameter Name="Subject5" Type="String" />
<asp:Parameter Name="Subject6" Type="String" />
<asp:Parameter Name="Subject7" Type="String" />
<asp:Parameter Name="TotalCreditHours" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Student_ID" Type="String" />
<asp:Parameter Name="Semester_ID" Type="String" />
<asp:Parameter Name="Course" Type="String" />
<asp:Parameter Name="Subject1" Type="String" />
<asp:Parameter Name="Subject2" Type="String" />
<asp:Parameter Name="Subject3" Type="String" />
<asp:Parameter Name="Subject4" Type="String" />
<asp:Parameter Name="Subject5" Type="String" />
<asp:Parameter Name="Subject6" Type="String" />
<asp:Parameter Name="Subject7" Type="String" />
<asp:Parameter Name="TotalCreditHours" Type="String" />
<asp:Parameter Name="original_Registration_ID" Type="String" />
<asp:Parameter Name="original_Student_ID" Type="String" />
<asp:Parameter Name="original_Semester_ID" Type="String" />
<asp:Parameter Name="original_Course" Type="String" />
<asp:Parameter Name="original_Subject1" Type="String" />
<asp:Parameter Name="original_Subject2" Type="String" />
<asp:Parameter Name="original_Subject3" Type="String" />
<asp:Parameter Name="original_Subject4" Type="String" />
<asp:Parameter Name="original_Subject5" Type="String" />
<asp:Parameter Name="original_Subject6" Type="String" />
<asp:Parameter Name="original_Subject7" Type="String" />
<asp:Parameter Name="original_TotalCreditHours" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Subject Registration SystemConnectionString %>" SelectCommand="SELECT DISTINCT [SubjectName] FROM [Subjects]"></asp:SqlDataSource>
<asp:Table runat="server">
<asp:TableRow>
<asp:TableCell>
<asp:Button ID="Button2" runat="server" Text="Back" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
 
 
My Code Behind:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Data;
using System.Configuration;
namespace Subject_Registration_System_for_IUMW
{
public partial class Approve_or_Reject : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=User-PC\\User;Initial Catalog=Subject Registration System;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] != null)
{
Label1.Text = Session["username"].ToString();
}
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Course FROM Subjects", con);
DDL1.DataSource = cmd.ExecuteReader();
DDL1.DataValueField = "Course";
DDL1.DataTextField = "Course";
DDL1.DataBind();
con.Close();
}
}
}
 
 
 Please help me 
Thank you 

Answers (4)