用的是属性生成器,按钮是pushbotton型
删除函数大概是这样写的protected void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TableCell itemCell1 = e.Item.Cells[0];
string item1 = itemCell1.Text;
this.sqlDataAdapter1.DeleteCommand.Parameters
["@Original_MyIdentity"].Value = item1; this.sqlDataAdapter1.DeleteCommand.Connection.Open();
this.sqlDataAdapter1.DeleteCommand.ExecuteNonQuery();
this.sqlDataAdapter1.DeleteCommand.Connection.Close();
sqlDataAdapter1.Fill(dataSet11);
DataGrid1.DataBind();
}
但是发现运行的时候没隔一次按起作用,而另一次只是刷新页面,没有删除数据
这是怎么回事啊?
删除函数大概是这样写的protected void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TableCell itemCell1 = e.Item.Cells[0];
string item1 = itemCell1.Text;
this.sqlDataAdapter1.DeleteCommand.Parameters
["@Original_MyIdentity"].Value = item1; this.sqlDataAdapter1.DeleteCommand.Connection.Open();
this.sqlDataAdapter1.DeleteCommand.ExecuteNonQuery();
this.sqlDataAdapter1.DeleteCommand.Connection.Close();
sqlDataAdapter1.Fill(dataSet11);
DataGrid1.DataBind();
}
但是发现运行的时候没隔一次按起作用,而另一次只是刷新页面,没有删除数据
这是怎么回事啊?
在这句 string item1 = itemCell1.Text;
设断点.
看 item1的值是不是每次都有值
我是菜鸟,用的是属性生成器和VS控件写的。
你的代码我对比了一下也没想到可能是哪里的问题~
2.如果不是,那么有可能是你的页面出现未知问题.解决办法如下:
新建一个页面,将你aspx页面中有用的代码拷过去(注意,只拷有用的代码)
将你aspx.cs页面里面,执行过程的代码拷到新页面去,然后,重新在aspx页面中,建立事件
如此,再运行看看.
晕~不过分页删除还是有问题,汗
但是这时候如果分页的话,切换页面就出了问题
我分页是这么写的
private void DataGrid1_PageIndexChanged(object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}
不过,象你上面所写的DataGrid1.DataBind();,这样行不通.你必须真正在分页里面重新对你的控件进行绑定.
另外,建议你使用成熟的分页控件,以下地址有一个,你可尝试看看.
www.webdiyer.com
{
SqlConnection myConnection = new SqlConnection("server=localhost;database=Test;uid=sa;pwd=123456");
SqlCommand myCommand = new SqlCommand("select * from person", myConnection);
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
MyDataGrid.DataSource = dr;
MyDataGrid.DataBind();
myConnection.Close();
}private void MyDataGrid_Delete(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SqlConnection myConnection = new SqlConnection("server=localhost;database=Test;uid=sa;pwd=123456");
SqlCommand delCommand = new SqlCommand("delete from person where id=@id", myConnection);
delCommand.Parameters.Add("@id",SqlDbType.Char,10);
delCommand.Parameters["@id"].Value =MyDataGrid.DataKeys[e.Item.ItemIndex];
myConnection.Open();
delCommand.ExecuteNonQuery();
myConnection.Close();
BindDataGrid(); }
以前在VC下面我就是那么做的,不过刚用.NET,摸不到头脑~