老问题~~又是gridview数据无法取得新值的问题~急
大家好!!我是新手,可是我为了这个问题已经一个礼拜睡不好了。哪位仁兄能拔刀相助一下呢??这个gridview在rowupdating事件里面就是取不到新值呢~~书上很多都是把控件一拉就得了。很少有介绍直接用代码来的,大家看一下我的程序吧。。。直接能运行的
******************
ASP.NET页面
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default5.aspx.cs " Inherits= "Default5 " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<fieldset>
<legend> 使用ObjectDataSource控件绑定数据访问层 </legend>
<table>
<tr>
<td> 选择地区: <asp:DropDownList ID= "DropDownList1 " runat= "server " AutoPostBack= "True " >
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:GridView ID= "GridView1 " runat= "server " AllowPaging= "True " PageSize= "5 " OnPageIndexChanging= "GridView1_PageIndexChanging " AutoGenerateEditButton= "True " OnRowEditing= "GridView1_RowEditing " OnRowUpdating= "GridView1_RowUpdating " AutoGenerateColumns= "False " >
<Columns>
<asp:BoundField DataField= "au_id " HeaderText= "au_id " />
<asp:BoundField DataField= "au_lname " HeaderText= "au_lname " />
<asp:BoundField DataField= "au_fname " HeaderText= "au_fname " />
<asp:BoundField DataField= "state " HeaderText= "state " />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>
***********************
页面后台Default5.aspx.cs
public partial class Default5 : System.Web.UI.Page
{
ObjectDataSource objds2;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ObjectDataSource objds1 = new ObjectDataSource( "Class1 ", "GetDropStates ");;
DropDownList1.DataSource = objds1;
DropDownList1.DataTextField = "state ";
DropDownList1.DataBind();
DropDownList1.SelectedIndex = 0;
GridView1.DataBind();
}
objds2 = new ObjectDataSource( "Class1 ", "GetAuthors ");
objds2.SelectParameters.Add( "state ", DropDownList1.SelectedItem.Text);
objds2.UpdateMethod = "UpdateAuthors ";
GridView1.DataSource = objds2;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
////
////就是在这边都是取到原来的值
//////
objds2.UpdateParameters.Add( "au_id ", ((TextBox)GridView1.Rows[e.RowIndex ].Cells[1].Controls[0]).Text);
objds2.UpdateParameters.Add( "au_lname ", ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
objds2.UpdateParameters.Add( "au_fname ", ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text);
objds2.UpdateParameters.Add( "state ", ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text);
objds2.Update();
GridView1.EditIndex = -1;
GridView1.DataBind();
}
}
*********************
数据访问层Class1.cs(也就是App_Code文件夹里的)
using System.Data.SqlClient;
public class Class1
{
public Class1()
{
}
public DataTable GetDropStates()
{
ConnectionStringSettings constr = ConfigurationManager.ConnectionStrings[ "pubConnectionString "];
if (constr != null)
{
string selstr = "select distinct state from authors ";
SqlConnection sqlcon = new SqlConnection(constr.ConnectionString);
SqlDataAdapter sqlad = new SqlDataAdapter(selstr, sqlcon);
DataTable dt = new DataTable( "state ");
sqlad.Fill(dt);
return dt;
}
else
return null;
}
public DataTable GetAuthors(string state)
{
ConnectionStringSettings constr = ConfigurationManager.ConnectionStrings[ "pubConnectionString "];
if (constr != null)
{
string selstr = "select au_id,au_lname,au_fname,state from authors where state=@state ";
SqlConnection sqlcon = new SqlConnection(constr.ConnectionString);
SqlCommand sqlcom = new SqlCommand();
sqlcom.Connection = sqlcon;
sqlcom.CommandType = CommandType.Text;
sqlcom.CommandText = selstr;
sqlcom.Parameters.Add( "@state ", SqlDbType.Char).Value = state;
SqlDataAdapter sqlad = new SqlDataAdapter(sqlcom);
DataTable dt = new DataTable( "authors ");
sqlad.Fill(dt);
return dt;
}
else
return null;
}
public int UpdateAuthors(string au_id,string au_lname,string au_fname,string state)
{
ConnectionStringSettings constr = ConfigurationManager.ConnectionStrings[ "pubConnectionString "];
if (constr != null)
{
string selstr = "update authors set au_lname=@au_lname,au_fname=@au_fname,state=@state where au_id=@au_id ";
SqlConnection sqlcon = new SqlConnection(constr.ConnectionString);
SqlCommand sqlcom = new SqlCommand();
sqlcom.Connection = sqlcon;
sqlcom.CommandType = CommandType.Text;
sqlcom.CommandText = selstr;
sqlcom.Parameters.Add( "@au_lname ", SqlDbType.Char).Value = au_lname;
sqlcom.Parameters.Add( "@au_fname ", SqlDbType.Char).Value = au_fname;
sqlcom.Parameters.Add( "@state ", SqlDbType.Char).Value = state;
sqlcom.Parameters.Add( "@au_id ", SqlDbType.Char).Value = au_id;
sqlcon.Open();
int rowaffect=sqlcom.ExecuteNonQuery();
sqlcon.Close();
return rowaffect;
}
else
return -1;
}
}
******************
Web.config文件
<?xml version= "1.0 "?>
<configuration>
<appSettings/>
<connectionStrings>
<add name= "pubConnectionString " connectionString= "data source=(local);database=pubs;integrated security=sspi "/>
</connectionStrings>
<system.web>
<httpRuntime maxRequestLength= "6000 " />
<compilation debug= "true "/>
<authentication mode= "Windows "/>
</system.web>
</configuration>
[解决办法]
objds2.UpdateParameters.Add( "au_id ", ……
程序没看出什么问题,是不是参数有问题,对于SqlServer一般都为@au_id
[解决办法]
把抛出异常的详细信息贴出来,
[解决办法]
GridView用法
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindToGridView();
}
}
//定义绑定
private void BindToGridView()
{
SqlConnection conn = DB.Createconn();
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand( "select * from newstype order by typeID ", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "newstype ");
this.GridView1.DataKeyNames = new string[] { "typeid " };
this.GridView1.DataSource = ds.Tables[ "newstype "];
this.GridView1.DataBind();
conn.Close();
}
//添加记录
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection conn = DB.Createconn();
conn.Open();
SqlCommand cmd = new SqlCommand( "insert into newsType(typeName) values( ' "+this.txtTypeName.Text+ " ') ",conn);
cmd.ExecuteNonQuery();
conn.Close();
this.txtTypeName.Text = " ";
this.BindToGridView();
}
//分页
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.BindToGridView();
}
//编辑记录
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
this.BindToGridView();
}
//更新记录
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox tb = (TextBox)GridView1.Rows[e.RowIndex].FindControl( "TextBox1 ");
string typeId = this.GridView1.DataKeys[e.RowIndex].Values[0].ToString();
SqlConnection conn = DB.Createconn();
conn.Open();
SqlCommand cmd = new SqlCommand( "update newstype set typeName= ' "+tb.Text+ " ' where typeid= " + typeId, conn);
cmd.ExecuteNonQuery();
conn.Close();
this.GridView1.EditIndex = -1;
this.BindToGridView();
}
//删除记录
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string typeId = this.GridView1.DataKeys[e.RowIndex].Values[0].ToString();
SqlConnection conn = DB.Createconn();
conn.Open();
SqlCommand cmd = new SqlCommand( "delete from newstype where typeid= "+typeId, conn);
cmd.ExecuteNonQuery();
conn.Close();
this.BindToGridView();
}
//编辑时的取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
this.BindToGridView();
}
//排序处理程序
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, "DESC ");
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, "ASC ");
}
}
//存储选定列当前排序状态
public SortDirection GridViewSortDirection
{
get
{
if (ViewState[ "sortDirection "] == null)
ViewState[ "sortDirection "] = SortDirection.Ascending;
return (SortDirection)ViewState[ "sortDirection "];
}
set { ViewState[ "sortDirection "] = value; }
}
/// 排序并绑定
private void SortGridView(string sortExpression, string direction)
{
SqlConnection conn = DB.Createconn();
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand( "select * from newstype order by typeID ", conn);
DataSet ds = new DataSet();
sda.Fill(ds, "newstype ");
DataTable dt = ds.Tables[ "newstype "];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + " " + direction;
GridView1.DataSource = dv;
GridView1.DataBind();
conn.Close();
}
//删除加确认
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[3].Attributes.Add( "onclick ", "Javascript:return confirm( '确认删除\ " "+e.Row.Cells[1].Text+ "\ "吗? ') ");
}
}
}