这就是我的gridview的样子:
基本上,我选择“你”作为正确答案。
但是点击“+”按钮后,单选按钮被删除了。
如何为gridview的每一行保留我选择的单选按钮?
这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
dropActivity(); // ignore this , this is for drop down list
dropTask(); // ignore this , this is for drop down list
}
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Question", typeof(string)));
dt.Columns.Add(new DataColumn("Hints No.1", typeof(string)));
dt.Columns.Add(new DataColumn("Hints No.2", typeof(string)));
dr = dt.NewRow();
dr["Question"] = string.Empty;
dr["Hints No.1"] = string.Empty;
dr["Hints No.2"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Hints No.1"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Hints No.2"] = box3.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
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++)
{
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
RadioButton radiobtn1 = (RadioButton)GridView1.Rows[i].Cells[2].FindControl("RadioButton1");
RadioButton radiobtn2 = (RadioButton)GridView1.Rows[i].Cells[3].FindControl("RadioButton2");
//Setting previous text to the respective textboxes based on columns.
box1.Text = dt.Rows[i]["Question"].ToString();
box2.Text = dt.Rows[i]["Hints No.1"].ToString();
box3.Text = dt.Rows[i]["Hints No.2"].ToString();
if (radiobtn1.Checked == true)
{
ViewState["RadioButtonStatus"] = radiobtn1.Checked;
}
if (radiobtn2.Checked == true)
{
ViewState["RadioButtonStatus"] = radiobtn2.Checked;
}
Session["Question1"] = box1.Text;
rowIndex++;
}
}
}
}
基本上我需要为我创建的每一行保留选择单选按钮。
答案 0 :(得分:0)
考虑更改“+”按钮以在客户端触发事件,而不是将新行发布到服务器。然后,当您将其发布到服务器上的“保存”事件处理程序时,您可以循环遍历该集合。
目前似乎正在发生的是你点击“+”按钮的回发。回发将导致清除那些单选按钮。我看到你试图通过ViewState保留状态,但是你要保存相同ViewState属性的已检查状态。
请参阅下面代码中的差异。
if (radiobtn1.Checked == true)
{
ViewState["RadioButtonStatus"] = radiobtn1.Checked;
}
if (radiobtn2.Checked == true)
{
ViewState["DifferentRadioButtonStatus"] = radiobtn2.Checked;
}
以下是AddNewRow方法的一些更新。这里有很多东西,这可能有点过度设计。希望它能帮助你朝着正确的方向前进。
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
var drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows.Add(drCurrentRow);
// Updated from > 0 to > 1 since the first action you're taking is in GridView1.Rows[1] via the for loop below
if (dtCurrentTable.Rows.Count > 1)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Hints No.1"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Hints No.2"] = box3.Text;
rowIndex++;
}
ViewState["CurrentTable"] = dtCurrentTable;
GridView1.DataSource = dtCurrentTable;
GridView1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
// Set Previous Data on Postbacks
SetPreviousData();
}