GridView控件的删除更新问题
先执行更新后执行删除没有异常,但是如果直接执行删除就会出现索引超出范围.求高手解答.publicpartialclassUserEditProduct:System.We...
先执行更新后执行删除没有异常,但是如果直接执行删除就会出现索引超出范围.求高手解答.
public partial class UserEditProduct : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && Session["Shop"] != null)
{
GridViewBind();
string sql = "select * from PsInfo where PsCuserName='" + Session["Shop"].ToString() + "'";
if (Summer.DataBindNull(sql))
{
this.Panel1.Visible = true;
}
}
}
protected void GridViewBind()
{
string sql = "select * from PsInfo where PsCuserName='"+Session["Shop"].ToString()+"' order by PsDateTime desc";
DataBindPs.DataSource = Summer.DTPsInfo(sql);
DataBindPs.DataBind();
DataBindPs.DataKeyNames = new string[] { "PsID" };
}
protected void DataBindPs_RowEditing(object sender, GridViewEditEventArgs e)
{
DataBindPs.EditIndex = e.NewEditIndex;
GridViewBind();
}
protected void DataBindPs_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
string NowTime = System.DateTime.Now.ToString();
string ProName = ((TextBox)(DataBindPs.Rows[e.RowIndex].Cells[0].Controls[0])).Text.Trim();
string ProPre = ((TextBox)(DataBindPs.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim();
if (Summer.ProductInsert("update PsInfo set PsName='" + ProName + "',PsPresent='" + ProPre + "',PsDateTime='" + NowTime + "' where PsID=" + this.DataBindPs.DataKeys[e.RowIndex].Value.ToString()))
{
DataBindPs.EditIndex = -1;
GridViewBind();
}
}
catch
{
Response.Write("<script language='javascript'>alert('您是不是输入字符太多了?')</script>");
}
}
protected void DataBindPs_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (Summer.ProductInsert("delete from PsInfo where PsID=" + this.DataBindPs.DataKeys[e.RowIndex].Value.ToString()))
{
GridViewBind();
}
}
protected void DataBindPs_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
DataBindPs.EditIndex = -1;
GridViewBind();
}
}
DataBindPs是GRIDVIEW控件ID 展开
public partial class UserEditProduct : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && Session["Shop"] != null)
{
GridViewBind();
string sql = "select * from PsInfo where PsCuserName='" + Session["Shop"].ToString() + "'";
if (Summer.DataBindNull(sql))
{
this.Panel1.Visible = true;
}
}
}
protected void GridViewBind()
{
string sql = "select * from PsInfo where PsCuserName='"+Session["Shop"].ToString()+"' order by PsDateTime desc";
DataBindPs.DataSource = Summer.DTPsInfo(sql);
DataBindPs.DataBind();
DataBindPs.DataKeyNames = new string[] { "PsID" };
}
protected void DataBindPs_RowEditing(object sender, GridViewEditEventArgs e)
{
DataBindPs.EditIndex = e.NewEditIndex;
GridViewBind();
}
protected void DataBindPs_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
string NowTime = System.DateTime.Now.ToString();
string ProName = ((TextBox)(DataBindPs.Rows[e.RowIndex].Cells[0].Controls[0])).Text.Trim();
string ProPre = ((TextBox)(DataBindPs.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim();
if (Summer.ProductInsert("update PsInfo set PsName='" + ProName + "',PsPresent='" + ProPre + "',PsDateTime='" + NowTime + "' where PsID=" + this.DataBindPs.DataKeys[e.RowIndex].Value.ToString()))
{
DataBindPs.EditIndex = -1;
GridViewBind();
}
}
catch
{
Response.Write("<script language='javascript'>alert('您是不是输入字符太多了?')</script>");
}
}
protected void DataBindPs_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (Summer.ProductInsert("delete from PsInfo where PsID=" + this.DataBindPs.DataKeys[e.RowIndex].Value.ToString()))
{
GridViewBind();
}
}
protected void DataBindPs_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
DataBindPs.EditIndex = -1;
GridViewBind();
}
}
DataBindPs是GRIDVIEW控件ID 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询