Insert, Delete, Update in GridView using ASP.NET
Submitted by sadiq.ameen.982 on Monday, September 15, 2014 - 22:52.
Language
1. As we don't want to use datasource controls. Please delete "ObjectDataSource1" control from the webform.
2. Delete DataSourceID="ObjectDataSource1" from GridView1. This should remove the dependency of GridVIew1 on ObjectDataSource1 control.
3. From the code behind file, delete lbInsert_Click() event handler method.
4. In the "FooterTemplate" of "EmployeeId" TemplateField, please delete OnClick="lbInsert_Click", as we no longer have this event handler method.
5. Delete "CommandField" column from GridView1
6. Now, include a template field in the place of CommandField. This template field is used to display Edit, Update, Cancel and Delete link buttons. We don't want delete and cancel buttons to cause validation, so set CausesValidaion property of these buttons to false.
7. Copy and paste the following private method. This method binds employee data with gridview1 control.
8. Call BindGridViewData() in Page_Load() event.
9. Finally generate GridView1_RowCommand() event handler method. Copy and Paste the following code.
10. In the FooterTemplate of EmployeeId TemplateField, set CommandName property lbInsert link button to "InsertRow"
11. If you want to show a confirmation dialog box, before a row is deleted, include javascript confirm() function, using "OnClientClick" attribute of LinkButton "lbDelete".
- <asp:TemplateField>
- <ItemTemplate>
- <asp:LinkButton ID="lbEdit" CommandArgument='<%# Eval("EmployeeId") %>' CommandName="EditRow" ForeColor="#8C4510" runat="server">Edit</asp:LinkButton>
- <asp:LinkButton ID="lbDelete" CommandArgument='<%# Eval("EmployeeId") %>' CommandName="DeleteRow" ForeColor="#8C4510" runat="server" CausesValidation="false">Delete</asp:LinkButton>
- </ItemTemplate>
- <EditItemTemplate>
- <asp:LinkButton ID="lbUpdate" CommandArgument='<%# Eval("EmployeeId") %>' CommandName="UpdateRow" ForeColor="#8C4510" runat="server">Update</asp:LinkButton>
- <asp:LinkButton ID="lbCancel" CommandArgument='<%# Eval("EmployeeId") %>' CommandName="CancelUpdate" ForeColor="#8C4510" runat="server" CausesValidation="false">Cancel</asp:LinkButton>
- </EditItemTemplate>
- </asp:TemplateField>
- private void BindGridViewData()
- {
- GridView1.DataSource = EmployeeDataAccessLayer.GetAllEmployees();
- GridView1.DataBind();
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- BindGridViewData();
- }
- }
- protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
- {
- if (e.CommandName == "EditRow")
- {
- int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
- GridView1.EditIndex = rowIndex;
- BindGridViewData();
- }
- else if (e.CommandName == "DeleteRow")
- {
- EmployeeDataAccessLayer.DeleteEmployee(Convert.ToInt32(e.CommandArgument));
- BindGridViewData();
- }
- else if (e.CommandName == "CancelUpdate")
- {
- GridView1.EditIndex = -1;
- BindGridViewData();
- }
- else if (e.CommandName == "UpdateRow")
- {
- int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
- int employeeId = Convert.ToInt32(e.CommandArgument);
- string name = ((TextBox)GridView1.Rows[rowIndex].FindControl("TextBox1")).Text;
- string gender = ((DropDownList)GridView1.Rows[rowIndex].FindControl("DropDownList1")).SelectedValue;
- string city = ((TextBox)GridView1.Rows[rowIndex].FindControl("TextBox3")).Text;
- EmployeeDataAccessLayer.UpdateEmployee(employeeId, name, gender, city);
- GridView1.EditIndex = -1;
- BindGridViewData();
- }
- else if (e.CommandName == "InsertRow")
- {
- string name = ((TextBox)GridView1.FooterRow.FindControl("txtName")).Text;
- string gender = ((DropDownList)GridView1.FooterRow.FindControl("ddlGender")).SelectedValue;
- string city = ((TextBox)GridView1.FooterRow.FindControl("txtCity")).Text;
- EmployeeDataAccessLayer.InsertEmployee(name, gender, city);
- BindGridViewData();
- }
- }
- <asp:LinkButton ID="lbInsert" CommandName="InsertRow" ForeColor="#8C4510" ValidationGroup="Insert" runat="server">Insert</asp:LinkButton>
- <asp:LinkButton ID="lbDelete" CommandArgument='<%# Eval("EmployeeId") %>' CommandName="DeleteRow" ForeColor="#8C4510" runat="server" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this row');">Delete</asp:LinkButton>
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 237 views