Viewstate 添加、删除行
Viewstate add,delete row
我在从 GridView 添加和删除行时遇到 2 个问题第一个:- 当我添加一些行时,它会自动在第一行添加空白行。我使用的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
initial();
//bill_date.Text = DateTime.Now.ToShortDateString();
add_bill_GridView.DataSource = null;
add_bill_GridView.DataBind();
using (SupermarketEntities1 db = new SupermarketEntities1())
{
ddl_choose_item.DataSource = db.Items.ToList();
ddl_choose_item.DataTextField = "item_name";
ddl_choose_item.DataValueField = "item_id";
ddl_choose_item.DataBind();
}
}
}
//this put on page_load
private void initial()
{
//creating DataTable
DataTable dt = new DataTable();
DataRow dr;
dt.TableName = "ProductsSold";
//creating columns for DataTable
dt.Columns.Add("ddl_choose_item", typeof(string));
dt.Columns.Add("txt_price", typeof(string));
dt.Columns.Add("txt_discount", typeof(string));
dt.Columns.Add("txt_quantitiy", typeof(string));
dt.Columns.Add("txt_total", typeof(string));
dr = dt.NewRow();
dt.Rows.Add(dr);
ViewState["ProductsSold"] = dt;
add_bill_GridView.DataSource = dt;
add_bill_GridView.DataBind();
}
//this put in btn_add_click
private void add_new_row()
{
if (ViewState["ProductsSold"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["ProductsSold"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["ddl_choose_item"] = ddl_choose_item.SelectedItem;
drCurrentRow["txt_price"] = txt_price.Text;
drCurrentRow["txt_discount"] = txt_discount.Text;
drCurrentRow["txt_quantitiy"] = txt_quantitiy.Text;
drCurrentRow["txt_total"] = txt_total.Text;
}
//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["ProductsSold"] = dtCurrentTable;
//binding Gridview with New Row
add_bill_GridView.DataSource = dtCurrentTable;
add_bill_GridView.DataBind();
}
}
}
//btn_add_Click
protected void btn_add_Click(object sender, EventArgs e)
{
add_new_row();
}
第二个问题是当我添加一些行并想删除其中的一些并再次添加新行时,它工作正常,但是如果我删除了所有行我就不能再添加了。我使用的代码
protected void BindGrid()
{
add_bill_GridView.DataSource = ViewState["ProductsSold"] as DataTable;
add_bill_GridView.DataBind();
}
protected void add_bill_GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["ProductsSold"] as DataTable;
dt.Rows[index].Delete();
ViewState["ProductsSold"] = dt;
BindGrid();
}
最后一个问题:- 用 jquery 做更好还是像我做的那样?除了处理服务器和不处理服务器之外,它们之间的区别是什么?
谢谢
请看一下
初始():
private void initial()
{
//creating DataTable
DataTable dt = new DataTable();
DataRow dr;
dt.TableName = "ProductsSold";
//creating columns for DataTable
dt.Columns.Add("ddl_choose_item", typeof(string));
dt.Columns.Add("txt_price", typeof(string));
dt.Columns.Add("txt_discount", typeof(string));
dt.Columns.Add("txt_quantitiy", typeof(string));
dt.Columns.Add("txt_total", typeof(string));
//dr = dt.NewRow();
//dt.Rows.Add(dr);
ViewState["ProductsSold"] = dt;
add_bill_GridView.DataSource = dt;
add_bill_GridView.DataBind();
}
添加新行方法:
private void add_new_row()
{
if (ViewState["ProductsSold"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["ProductsSold"];
DataRow drCurrentRow;
//if (dtCurrentTable.Rows.Count > 0)
//{
//for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
//{
//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["ddl_choose_item"] = "ddl";
drCurrentRow["txt_price"] = "pric";
drCurrentRow["txt_discount"] = "dis";
drCurrentRow["txt_quantitiy"] = "quan";
drCurrentRow["txt_total"] = "tot";
//}
//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["ProductsSold"] = dtCurrentTable;
add_bill_GridView.DataSource = dtCurrentTable;
add_bill_GridView.DataBind();
//}
}
}
在上面的代码中,我注释了一些行,请检查并告诉我。
我发现了问题....
实际上您的视图状态永远不会变为空。因此,当您删除所有行 viewstate not == null 并且两个条件都变为 false 时。
解决方案:
只需设置一个条件
if (GridView1.Rows.Count == 0)
{
ViewState["ProductsSold"] = null;
}
如果仍然感到困惑,请告诉我...
我在从 GridView 添加和删除行时遇到 2 个问题第一个:- 当我添加一些行时,它会自动在第一行添加空白行。我使用的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
initial();
//bill_date.Text = DateTime.Now.ToShortDateString();
add_bill_GridView.DataSource = null;
add_bill_GridView.DataBind();
using (SupermarketEntities1 db = new SupermarketEntities1())
{
ddl_choose_item.DataSource = db.Items.ToList();
ddl_choose_item.DataTextField = "item_name";
ddl_choose_item.DataValueField = "item_id";
ddl_choose_item.DataBind();
}
}
}
//this put on page_load
private void initial()
{
//creating DataTable
DataTable dt = new DataTable();
DataRow dr;
dt.TableName = "ProductsSold";
//creating columns for DataTable
dt.Columns.Add("ddl_choose_item", typeof(string));
dt.Columns.Add("txt_price", typeof(string));
dt.Columns.Add("txt_discount", typeof(string));
dt.Columns.Add("txt_quantitiy", typeof(string));
dt.Columns.Add("txt_total", typeof(string));
dr = dt.NewRow();
dt.Rows.Add(dr);
ViewState["ProductsSold"] = dt;
add_bill_GridView.DataSource = dt;
add_bill_GridView.DataBind();
}
//this put in btn_add_click
private void add_new_row()
{
if (ViewState["ProductsSold"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["ProductsSold"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["ddl_choose_item"] = ddl_choose_item.SelectedItem;
drCurrentRow["txt_price"] = txt_price.Text;
drCurrentRow["txt_discount"] = txt_discount.Text;
drCurrentRow["txt_quantitiy"] = txt_quantitiy.Text;
drCurrentRow["txt_total"] = txt_total.Text;
}
//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["ProductsSold"] = dtCurrentTable;
//binding Gridview with New Row
add_bill_GridView.DataSource = dtCurrentTable;
add_bill_GridView.DataBind();
}
}
}
//btn_add_Click
protected void btn_add_Click(object sender, EventArgs e)
{
add_new_row();
}
第二个问题是当我添加一些行并想删除其中的一些并再次添加新行时,它工作正常,但是如果我删除了所有行我就不能再添加了。我使用的代码
protected void BindGrid()
{
add_bill_GridView.DataSource = ViewState["ProductsSold"] as DataTable;
add_bill_GridView.DataBind();
}
protected void add_bill_GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["ProductsSold"] as DataTable;
dt.Rows[index].Delete();
ViewState["ProductsSold"] = dt;
BindGrid();
}
最后一个问题:- 用 jquery 做更好还是像我做的那样?除了处理服务器和不处理服务器之外,它们之间的区别是什么? 谢谢
请看一下
初始():
private void initial()
{
//creating DataTable
DataTable dt = new DataTable();
DataRow dr;
dt.TableName = "ProductsSold";
//creating columns for DataTable
dt.Columns.Add("ddl_choose_item", typeof(string));
dt.Columns.Add("txt_price", typeof(string));
dt.Columns.Add("txt_discount", typeof(string));
dt.Columns.Add("txt_quantitiy", typeof(string));
dt.Columns.Add("txt_total", typeof(string));
//dr = dt.NewRow();
//dt.Rows.Add(dr);
ViewState["ProductsSold"] = dt;
add_bill_GridView.DataSource = dt;
add_bill_GridView.DataBind();
}
添加新行方法:
private void add_new_row()
{
if (ViewState["ProductsSold"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["ProductsSold"];
DataRow drCurrentRow;
//if (dtCurrentTable.Rows.Count > 0)
//{
//for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
//{
//Creating new row and assigning values
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["ddl_choose_item"] = "ddl";
drCurrentRow["txt_price"] = "pric";
drCurrentRow["txt_discount"] = "dis";
drCurrentRow["txt_quantitiy"] = "quan";
drCurrentRow["txt_total"] = "tot";
//}
//Added New Record to the DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//storing DataTable to ViewState
ViewState["ProductsSold"] = dtCurrentTable;
add_bill_GridView.DataSource = dtCurrentTable;
add_bill_GridView.DataBind();
//}
}
}
在上面的代码中,我注释了一些行,请检查并告诉我。
我发现了问题....
实际上您的视图状态永远不会变为空。因此,当您删除所有行 viewstate not == null 并且两个条件都变为 false 时。
解决方案:
只需设置一个条件
if (GridView1.Rows.Count == 0)
{
ViewState["ProductsSold"] = null;
}
如果仍然感到困惑,请告诉我...