如何在回发后在 gridview 中保持状态 asp.net
How to keep state in gridview after a postback asp.net
我的程序工作正常,我的 gridview 在页面加载后加载等,但我遇到的问题是当我在编辑功能上单击编辑并输入一个新值并按更新时,我的程序转到空白屏幕,直到我再次从我的下拉列表中选择产品,它正在更新数据库,但我希望它保持其状态并在我单击更新后保持在同一页面上。这是我的代码。
public partial class Default : Page
{
private int Target { get; set; }
private string ProductShortDesc { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (ddlproduct.Items.Count == 0)
{
BindDropDownList();
RefreshGrid(ProductShortDesc);
}
}
}
private void BindDropDownList()
{
{
try
{
string[] productTexts;
string[] productValues;
BusinessManager biz = new BusinessManager();
biz.GetProductSeriesList(out productTexts, out productValues);
ddlproduct.Items.Clear();
int x = 0;
foreach (string s in productTexts)
{
ListItem li = new ListItem(s, productValues[x]);
x++;
ddlproduct.Items.Add(li);
}
}
catch (SqlException ex)
{
throw new Exception("Failed to get product items", ex);
}
catch (Exception ex)
{
throw new Exception("Failed to get product items:", ex);
}
}
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
ProductShortDesc = ddlproduct.SelectedValue;
RefreshGrid(ProductShortDesc);
}
public void RefreshGrid(string productShortDesc)
{
try
{
// get the list of records & bind to the grid
BusinessManager biz = new BusinessManager();
ProductShortDesc = ddlproduct.SelectedValue;
DataTable dt = new DataTable();
dt = biz.GetPackingShiftData(ProductShortDesc);
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}
catch (SqlException ex)
{
throw new Exception("Could not populate the list due to an SQL error:", ex);
}
catch (Exception ex)
{
throw new Exception("Application error in adding products to the list:", ex);
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Assign Target property Value
try
{
TextBox tb =
(TextBox) GridView1.Rows[e.RowIndex].FindControl("TargetTextBox"); //finds the target column
Target = int.Parse((tb.Text));
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
using (DataManager dmgr = new DataManager())
{
dmgr.Connect("PRODUCTION");
dmgr.PackingShiftTargetUpdate(id, Target);
dmgr.Disconnect();
}
GridView1.EditIndex = -1;
DataBind();
}
catch (SqlException msg)
{
throw new Exception("Input error:", msg);
}
catch (Exception ex)
{
throw new Exception("Only a Number input is allowed:", ex);
}
}
protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
{
try
{
RefreshGrid(ProductShortDesc);
GridView1.EditIndex = e.NewEditIndex;
DataBind();
}
catch (SqlException ex)
{
throw new Exception("Editing row error", ex);
}
catch (Exception ex)
{
throw new Exception("Application Error when editing application", ex);
}
}
protected void GridView1_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
try
{
//Reset the edit index.
GridView1.EditIndex = -1;
//Bind data to the GridView control.
DataBind();
}
catch (SqlException ex)
{
throw new Exception("error when editing row", ex);
}
catch (Exception ex)
{
throw new Exception("Application error when cancelling", ex);
}
}
protected void GridView1_OnRowUpdated(object sender, GridViewUpdatedEventArgs e)
{
GridView1.EditIndex = -1;
DataBind();
}
private void HandleSqlEx(SqlException ex, string Msg)
{
ExceptionLabel.ForeColor = Color.Red;
ExceptionLabel.Text = "SQL error:" + Msg;
}
private void HandleException(Exception ex, string Msg)
{
ExceptionLabel.ForeColor = Color.Red;
ExceptionLabel.Text = Msg;
}
}
}
完成更新 database
后,在 GridView1_RowUpdating
事件中调用您的 RefreshGrid
方法。
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Assign Target property Value
try
{
TextBox tb =
(TextBox) GridView1.Rows[e.RowIndex].FindControl("TargetTextBox"); //finds the target column
Target = int.Parse((tb.Text));
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
using (DataManager dmgr = new DataManager())
{
dmgr.Connect("PRODUCTION");
dmgr.PackingShiftTargetUpdate(id, Target);
dmgr.Disconnect();
}
GridView1.EditIndex = -1;
RefreshGrid(ProductShortDesc);
DataBind();
}
catch (SqlException msg)
{
throw new Exception("Input error:", msg);
}
catch (Exception ex)
{
throw new Exception("Only a Number input is allowed:", ex);
}
}
我的程序工作正常,我的 gridview 在页面加载后加载等,但我遇到的问题是当我在编辑功能上单击编辑并输入一个新值并按更新时,我的程序转到空白屏幕,直到我再次从我的下拉列表中选择产品,它正在更新数据库,但我希望它保持其状态并在我单击更新后保持在同一页面上。这是我的代码。
public partial class Default : Page
{
private int Target { get; set; }
private string ProductShortDesc { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (ddlproduct.Items.Count == 0)
{
BindDropDownList();
RefreshGrid(ProductShortDesc);
}
}
}
private void BindDropDownList()
{
{
try
{
string[] productTexts;
string[] productValues;
BusinessManager biz = new BusinessManager();
biz.GetProductSeriesList(out productTexts, out productValues);
ddlproduct.Items.Clear();
int x = 0;
foreach (string s in productTexts)
{
ListItem li = new ListItem(s, productValues[x]);
x++;
ddlproduct.Items.Add(li);
}
}
catch (SqlException ex)
{
throw new Exception("Failed to get product items", ex);
}
catch (Exception ex)
{
throw new Exception("Failed to get product items:", ex);
}
}
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
ProductShortDesc = ddlproduct.SelectedValue;
RefreshGrid(ProductShortDesc);
}
public void RefreshGrid(string productShortDesc)
{
try
{
// get the list of records & bind to the grid
BusinessManager biz = new BusinessManager();
ProductShortDesc = ddlproduct.SelectedValue;
DataTable dt = new DataTable();
dt = biz.GetPackingShiftData(ProductShortDesc);
GridView1.DataSource = dt.DefaultView;
GridView1.DataBind();
}
catch (SqlException ex)
{
throw new Exception("Could not populate the list due to an SQL error:", ex);
}
catch (Exception ex)
{
throw new Exception("Application error in adding products to the list:", ex);
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Assign Target property Value
try
{
TextBox tb =
(TextBox) GridView1.Rows[e.RowIndex].FindControl("TargetTextBox"); //finds the target column
Target = int.Parse((tb.Text));
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
using (DataManager dmgr = new DataManager())
{
dmgr.Connect("PRODUCTION");
dmgr.PackingShiftTargetUpdate(id, Target);
dmgr.Disconnect();
}
GridView1.EditIndex = -1;
DataBind();
}
catch (SqlException msg)
{
throw new Exception("Input error:", msg);
}
catch (Exception ex)
{
throw new Exception("Only a Number input is allowed:", ex);
}
}
protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
{
try
{
RefreshGrid(ProductShortDesc);
GridView1.EditIndex = e.NewEditIndex;
DataBind();
}
catch (SqlException ex)
{
throw new Exception("Editing row error", ex);
}
catch (Exception ex)
{
throw new Exception("Application Error when editing application", ex);
}
}
protected void GridView1_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
try
{
//Reset the edit index.
GridView1.EditIndex = -1;
//Bind data to the GridView control.
DataBind();
}
catch (SqlException ex)
{
throw new Exception("error when editing row", ex);
}
catch (Exception ex)
{
throw new Exception("Application error when cancelling", ex);
}
}
protected void GridView1_OnRowUpdated(object sender, GridViewUpdatedEventArgs e)
{
GridView1.EditIndex = -1;
DataBind();
}
private void HandleSqlEx(SqlException ex, string Msg)
{
ExceptionLabel.ForeColor = Color.Red;
ExceptionLabel.Text = "SQL error:" + Msg;
}
private void HandleException(Exception ex, string Msg)
{
ExceptionLabel.ForeColor = Color.Red;
ExceptionLabel.Text = Msg;
}
}
}
完成更新 database
后,在 GridView1_RowUpdating
事件中调用您的 RefreshGrid
方法。
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Assign Target property Value
try
{
TextBox tb =
(TextBox) GridView1.Rows[e.RowIndex].FindControl("TargetTextBox"); //finds the target column
Target = int.Parse((tb.Text));
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
using (DataManager dmgr = new DataManager())
{
dmgr.Connect("PRODUCTION");
dmgr.PackingShiftTargetUpdate(id, Target);
dmgr.Disconnect();
}
GridView1.EditIndex = -1;
RefreshGrid(ProductShortDesc);
DataBind();
}
catch (SqlException msg)
{
throw new Exception("Input error:", msg);
}
catch (Exception ex)
{
throw new Exception("Only a Number input is allowed:", ex);
}
}