【发布时间】:2014-03-25 03:31:16
【问题描述】:
我正在尝试使用网格视图来显示数据库中的项目我还想编辑和删除这些记录然后在网格视图中,我正在使用 asp.net sn-ps 上的示例代码
它说名称'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 Name=@Name," +
"Password=@Password where EmployeeID=@EmployeeID;" +
"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 " +
"EmployeeID=@EmployeeID;" +
"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 只需检查我的代码并替换为您的完整 coed..
-
@user3395738 告诉我你的代码是否有任何实现问题..