0
我有一個數據條目表,每行都有一個AJAX組合框和一個HTML文本輸入。我還沒有能夠通過Javascript動態地添加組合框,所以我做了一個「添加行」按鈕。在最初添加行以填充數據工作時,button-fired事件刪除這些行,因此表只包含新行。這兩種方法都使用Table.Rows.Add(row),並且通過用於初始和按鈕觸發行添加的相同方法來生成行。將行添加到ASP.NET表中會覆蓋以前的行
這裏是我的代碼生成行:
protected System.Web.UI.HtmlControls.HtmlTableRow getEmptyRow(int rowNum, bool script)
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell a = new HtmlTableCell();
a.Style.Add("width", "75%");
var chgsarray = d.WaterRates.Select(q => q.Name)
.Where(q => !q.Contains("test"))
.OrderBy(q => q);
AjaxControlToolkit.ComboBox t = new AjaxControlToolkit.ComboBox() { ID = "chrgName" + rowNum, AutoPostBack = false, DataSource = chgsarray, AutoCompleteMode = AjaxControlToolkit.ComboBoxAutoCompleteMode.SuggestAppend, ItemInsertLocation = AjaxControlToolkit.ComboBoxItemInsertLocation.Append, DropDownStyle = AjaxControlToolkit.ComboBoxStyle.DropDown };
t.DataBind();
t.Text = "";
a.Controls.Add(t);
row.Cells.Add(a);
a = new HtmlTableCell();
a.Style.Add("width", "25%");
a.InnerText = "$";
HtmlInputText tc = new HtmlInputText() { ID = "chrgChrg" + rowNum, Name = "chrgChrg" + rowNum, ViewStateMode = ViewStateMode.Disabled, ClientIDMode = ClientIDMode.Static };
tc.Style.Add("width", "182px");
a.Controls.Add(tc);
row.Cells.Add(a);
return row;
}
和代碼在按下按鈕添加該行是很簡單的:
protected void Add_Row(Object sender, EventArgs e)
{
int k = -1;
for (int i = 0; Request.Form.AllKeys.Contains("ctl00$MainContent$chrgChrg" + i); i++)
k++;
ChargesTable.Rows.Add(getEmptyRow(k+1, true));
}
所以,我怎麼能當添加保留現有的行一個新的行?
這樣做很有意義,謝謝! – user1488654