我正在使用grid-view
两个text-box
控件和一个checkboxlist
控件。当我在grid-view
动态添加新行时,两个text-box
包含上一行中的值,但checkboxlist
中的项控制在上一行中未选中。我想维护上一行中checkboxlist
的状态。这该怎么做?任何人都可以提出任何建议吗?
protected void Page_Load(object sender, EventArgs e)
{
SetInitialRow();
}
public void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
//Define the Columns
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
//Add a Dummy Data on Initial Load
dr = dt.NewRow();
dr["RowNumber"] = 1;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
//Bind the DataTable to the Grid
Gridview1.DataSource = dt;
Gridview1.DataBind();
//Extract and Fill the DropDownList with Data
TextBox txt1 = (TextBox)Gridview1.Rows[0].Cells[1].FindControl("TextBox1");
TextBox txt2 = (TextBox)Gridview1.Rows[0].Cells[2].FindControl("TextBox2");
CheckBoxList chk1 = (CheckBoxList)Gridview1.Rows[0].Cells[3].FindControl("checkBoxes1");
}
private void AddNewRowToGrid()
{
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
//add new row to DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Store the current data to ViewState
ViewState["CurrentTable"] = dtCurrentTable;
for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
{
//extract the DropDownList Selected Items
TextBox txt1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("TextBox1");
TextBox txt2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox2");
CheckBoxList chk1 = (CheckBoxList)Gridview1.Rows[i].Cells[3].FindControl("checkBoxes1");
// Update the DataRow with the DDL Selected Items
dtCurrentTable.Rows[i]["Column1"] = txt1.Text;
dtCurrentTable.Rows[i]["Column2"] =txt2.Text;
dtCurrentTable.Rows[i]["Column3"] = chk1.SelectedValue;
}
//Rebind the Grid with the current data
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
//Set the Previous Selected Items on Each DropDownList on Postbacks
TextBox txt1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox txt2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
CheckBoxList chk1 = (CheckBoxList)Gridview1.Rows[rowIndex].Cells[3].FindControl("checkBoxes1");
if (i < dt.Rows.Count - 1)
{
txt1.Text = dt.Rows[i]["Column1"].ToString();
txt2.Text = dt.Rows[i]["Column2"].ToString();
chk1.SelectedValue = dt.Rows[i]["Column3"].ToString();
}
rowIndex++;
}
}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}