0
我有一個網格視圖,用戶將不得不在文本框中輸入的東西和wadever用戶類型將被插入到數據庫中,我有這個問題或問題,每次我點擊「+」按鈕(添加新聞行),前一行數據的文本框中的一個將被刪除。所選的單選按鈕也將被刪除。繼承人是截圖的問題是什麼:在網格視圖設置以前的數據
點擊「+」按鈕後,它會變成是這樣的:
這是我的代碼背後:
private void AddNewRowToGrid()
{
int rowIndex = 0;
CheckBox chkbox1 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox1");
CheckBox chkbox2 = (CheckBox)GridView1.Rows[rowIndex].Cells[2].FindControl("CheckBox2");
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[2].FindControl("TextBox3");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["Question"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Hints"] = 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 SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Question", typeof(string)));
dt.Columns.Add(new DataColumn("Hints", typeof(string)));
dr = dt.NewRow();
dr["Question"] = string.Empty;
dr["Hints"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
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[2].FindControl("TextBox3");
//Setting previous text to the respective textboxes based on columns.
box1.Text = dt.Rows[i]["Question"].ToString();
box2.Text = dt.Rows[i]["Hints"].ToString();
box3.Text = dt.Rows[i]["Hints"].ToString();
Session["Question1"] = box1.Text;
rowIndex++;
}
}
}
}
我可以很容易地做到這一點,只有每個列上有一個文本框..但現在每個列上有2個文本框列......因爲我只能將文本追加到基於列名的文本框中......另外,如何維護我的單選按鈕幫助表示感謝,謝謝!
問題是,您正在將兩個文本框值保存到一個數據表列。數據表中有兩列,每個文本框一列最好。 HintsOne,HintsTwo或者使用一些分隔符來合併兩個文本框的值,並將其放入數據表的一列(提示)中。然後將它們分開,然後顯示到Gridview中...它不是那麼好,所以更喜歡第一種方法(使用兩列) – Munawar
是的,我認爲這是一種選擇,但我想這樣工作.. duno如果可能.. – user2376998
一列將始終具有單個文本框的值,除非在放入數據列之前合併兩個文本框中的值。 dt.Rows [I] [ 「提示」]的ToString();將始終返回單個值(最有可能是您從textbox3保存的任何值)因此,您可以選擇將兩個數據列合併,然後再將其存儲到數據表中,然後再將這些值分開,然後再放回文本框 – Munawar