删除命令是工作,但显示一个空白页后,按钮单击
本文关键字:空白 一个 按钮 单击 命令 工作 显示 删除 | 更新日期: 2023-09-27 18:04:43
我有一个add product函数。添加产品后,我有一个链接,将在GridView中查看所有添加的产品。这两个函数运行得很好。在查看产品页面中,我有一个编辑和删除功能。两者都工作得很好,但当我尝试点击删除并点击确定时,它向我显示了一个空白页面(但当我试图回到页面时,产品被删除了)任何想法我在这里错过了什么?
这是viewproducts页面的代码: <html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="ProductID"
OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" />
<asp:BoundField DataField="ProductDescription" HeaderText="ProductDescription" />
<asp:ImageField HeaderText ="ProductImage" DataImageUrlField="ProductImage" SortExpression="ProductImage" ControlStyle-Width ="10">
<ControlStyle Width="50px"></ControlStyle>
</asp:ImageField>
<asp:BoundField DataField="ProductQuantity" HeaderText="ProductQuantity" />
<asp:BoundField DataField="ProductPrice" HeaderText="ProductPrice" />
<asp:CommandField ShowEditButton="true" />
<%--<asp:CommandField ShowDeleteButton="true" />--%>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkdel" runat="server" Text="Delete" CommandName="Delete"
OnClientClick="return confirm('Confirm Delete?');"></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
背后的代码:
namespace WebApplication5
{
public partial class WebForm2 : System.Web.UI.Page
{
private SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=Authorship;Integrated Security =True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvbind();
}
}
protected void gvbind()
{
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from Products", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int prodid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["ProductID"].ToString());
SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=Authorship;Integrated Security =True");
SqlDataAdapter da = new SqlDataAdapter("", conn);
conn.Open();
da.DeleteCommand = new SqlCommand("delete from Products where ProductID=" + prodid, conn);
da.DeleteCommand.ExecuteNonQuery();
conn.Close();
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gvbind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");
//TextBox txtname=(TextBox)gr.cell[].control[];
TextBox textName = (TextBox)row.Cells[0].Controls[0];
TextBox textDesc = (TextBox)row.Cells[1].Controls[0];
TextBox textQuantity = (TextBox)row.Cells[3].Controls[0];
TextBox textProductPrice = (TextBox)row.Cells[4].Controls[0];
//TextBox textadd = (TextBox)row.FindControl("txtadd");
//TextBox textc = (TextBox)row.FindControl("txtc");
GridView1.EditIndex = -1;
conn.Open();
//SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
SqlCommand cmd = new SqlCommand("update Products set ProductName='" + textName.Text + "',ProductDescription='" + textDesc.Text + "',ProductQuantity='" + textQuantity.Text + "',ProductPrice ='" + textProductPrice.Text + "'where ProductID='" + userid + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();
//GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
gvbind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
gvbind();
}
}
}
你需要将数据源重新绑定回gridview,所以在你的代码中它将是这样的
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int prodid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["ProductID"].ToString());
SqlConnection conn = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=Authorship;Integrated Security =True");
SqlDataAdapter da = new SqlDataAdapter("", conn);
conn.Open();
da.DeleteCommand = new SqlCommand("delete from Products where ProductID=" + prodid, conn);
da.DeleteCommand.ExecuteNonQuery();
conn.Close();
//GridView1.DataBind();
gvbind();
}
所以根据上面的代码,你需要调用绑定datagrid的gvbind();
方法,而不是调用GridView1.DataBind();
方法。