0
我試圖使用網格視圖來顯示從數據庫中我也想要編輯,並在網格視圖刪除這些記錄,然後項目,我使用ASP的示例代碼。淨片段asp.net網格視圖編輯和刪除記錄
它說「的GetData」這個名字並不在當前的背景下
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default10 : System.Web.UI.Page
{
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
comm = new SqlCommand("select EmployeeID,Name,Password" +
" from Employee");
GridView1.DataSource = GetData(comm);
GridView1.DataBind();
}
protected void EditEmployee(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
protected void UpdateEmployee(object sender, GridViewUpdateEventArgs e)
{
string EmployeeID = ((Label)GridView1.Rows[e.RowIndex]
.FindControl("lblEmployeeID")).Text;
string Name = ((TextBox)GridView1.Rows[e.RowIndex]
.FindControl("txtName")).Text;
string Password = ((TextBox)GridView1.Rows[e.RowIndex]
.FindControl("txtPassword")).Text;
comm = new SqlCommand();
comm.CommandType = CommandType.Text;
comm.CommandText = "update Employee set [email protected]," +
"[email protected] where [email protected];" +
"select EmployeeID,Name,Password from Employee";
comm.Parameters.Add("@EmployeeID", SqlDbType.VarChar).Value = EmployeeID;
comm.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name;
comm.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password;
GridView1.EditIndex = -1;
GridView1.DataSource = GetData(comm);
GridView1.DataBind();
}
protected void DeleteEmployee(object sender, EventArgs e)
{
LinkButton lnkRemove = (LinkButton)sender;
comm = new SqlCommand();
comm.CommandType = CommandType.Text;
comm.CommandText = "delete from Employee where " +
"[email protected];" +
"select EmployeeID,Name,Password from Employee";
comm.Parameters.Add("@EmployeeID", SqlDbType.VarChar).Value
= lnkRemove.CommandArgument;
GridView1.DataSource = GetData(comm);
GridView1.DataBind();
}
}
我沒有看到任何的GetData()在你的代碼。你只有這個:GetData(comm)。方法聲明在哪裏? –
現在加入的GetData()函數與您的代碼,並檢查 –
@ user3395738只是檢查我的代碼,並與您的完整的男女同校代替.. – 2014-03-25 04:49:05