-1
請儘快回答我的問題....我真的需要它爲我的項目..這是我的代碼更新記錄。現在什麼樣的變化,我應該在我的代碼做刪除和添加新的記錄?? ..我想要做的只是我的代碼更改...如何在asp.net中的gridview中刪除和添加新記錄?
這是我Exams.aspx頁: -
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage2.master" AutoEventWireup="true" CodeFile="Exams.aspx.cs" Inherits="Exams" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
</div>
<div>
</div>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="6" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>
<asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("EXAM_Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam Title">
<ItemTemplate>
<asp:Label ID="lbl_title" runat="server" Text='<%#Eval("EXAM_TITLE") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_title" runat="server" Text='<%#Eval("EXAM_TITLE") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam Date">
<ItemTemplate>
<asp:Label ID="lbl_date" runat="server" Text='<%#Eval("EXAM_DATE") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_date" runat="server" Text='<%#Eval("EXAM_DATE") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam Time">
<ItemTemplate>
<asp:Label ID="lbl_time" runat="server" Text='<%#Eval("TOTAL_TIME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_time" runat="server" Text='<%#Eval("TOTAL_TIME") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exam Paper">
<ItemTemplate>
<asp:Label ID="lbl_paper" runat="server" Text='<%#Eval("GENERATED_EXAM_PAPER") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_paper" runat="server" Text='<%#Eval("GENERATED_EXAM_PAPER") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#663300" ForeColor="#ffffff"/>
<RowStyle BackColor="#e7ceb6"/>
</asp:GridView>
</div>
<div>
<asp:label runat="server" ID="label12"></asp:label>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
</asp:Content>
這是我Exams.aspx.cs頁: -
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.Configuration;
using System.Data;
public partial class Exams : System.Web.UI.Page
{
//Connection String from web.config File
string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con;
SqlDataAdapter adapt;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
ShowData();
}
}catch(Exception ex)
{
label12.Text = ex.Message;
}
}
//ShowData method for Displaying Data in Gridview
protected void ShowData()
{
dt = new DataTable();
con = new SqlConnection(cs);
con.Open();
adapt = new SqlDataAdapter("Select EXAM_Id,EXAM_TITLE,EXAM_DATE,TOTAL_TIME,GENERATED_EXAM_PAPER from EXAM_PAPER", con);
adapt.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Close();
}
protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
{
//NewEditIndex property used to determine the index of the row being edited.
GridView1.EditIndex = e.NewEditIndex;
ShowData();
}
protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
//Finding the controls from Gridview for the row which is going to update
Label id = GridView1.Rows[e.RowIndex].FindControl("lbl_ID") as Label;
TextBox title = GridView1.Rows[e.RowIndex].FindControl("txt_title") as TextBox;
TextBox date = GridView1.Rows[e.RowIndex].FindControl("txt_date") as TextBox;
TextBox time = GridView1.Rows[e.RowIndex].FindControl("txt_time") as TextBox;
TextBox paper = GridView1.Rows[e.RowIndex].FindControl("txt_paper") as TextBox;
con = new SqlConnection(cs);
con.Open();
//updating the record
SqlCommand cmd = new SqlCommand("Update EXAM_PAPER set EXAM_TITLE='" + title.Text + "',EXAM_DATE='" + date.Text + "',TOTAL_TIME='" + time.Text + "',GENERATED_EXAM_PAPER='" + paper.Text + "' where EXAM_Id=" + Convert.ToInt32(id.Text), con);
cmd.ExecuteNonQuery();
con.Close();
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
//Call ShowData method for displaying updated data
ShowData();
}
protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
{
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
ShowData();
}
}
閱讀這篇文章http://www.aspsnippets.com/Articles/Simple-Insert-Select-Edit-Update-and-Delete-in-ASPNet-GridView-control.aspx –
你應該對GridView1_RowInserting和GridView1_RowDeleting執行相同的操作並在.aspx中添加OnRowInserting和OnRowDeleting事件 –
您是否想對網格執行添加/刪除操作?或者你是否也希望將該事務保存在數據庫中?請確認。 –