0
將表單數據寫入數據庫時出現問題。通過克隆表單控件並增加文本框名稱和id屬性,即時創建多行
。但是,當我發佈表單時只有第一行寫入數據庫。提交具有多行的動態表單
var ind = Request.Form.Count;
foreach(int i = 0; i < ind; i++){
if (IsPost)
{
Item_No = Request.Form.Item_No[i];
Item_Desc = Request.Form.Item_Desc[i];
Qty = Request.Form.Qty[i];
var db = Database.Open("s2k");
var insertQuery = "INSERT INTO OrderItems" +
"(Item_No, Item_Desc, Qty)" +
"Values (@0, @1, @2)";
db.Execute(insertQuery, Item_No, Item_Desc, Qty);
Response.Redirect("~");
}
}
增加了新的表單元素
function addTableRow(table)
{
var $tr = $(table).find("tbody tr:last").clone();
$tr.find("input,select").val('').attr("name", function()
{
var parts = this.id.match(/(\D+)(\d+)$/);
return parts[1] + ++parts[2];
}).attr("id", function(){
var parts = this.id.match(/(\D+)(\d+)$/);
return parts[1] + ++parts[2];
});
$(table).find("tbody tr:last").after($tr);
};
形式
<form method="post" id = "form" title= "form">
<table>
<thead>
<tr>
<th class="product">Item_No</th>
<th class="size">Item_Desc</th>
<th class="price">Qty</th>
</tr>
</thead>
<tbody>
<td><input type="text" name="Item_No" id="Item_No1"></td>
<td><input type="text" name="Item_Desc" id="Item_Desc1"></td>
<td><input type="number" name="Qty" id="Qty1"></td>
</tr>
</tbody>
</table>
<p><input type="submit" name="buttonSubmit" value="Create Order" /></p>
</form>
大聲笑..感謝...沒有注意到...但循環會通過形式如何設置語法明智.. – Zion 2013-03-01 19:40:40
@Zion我不熟悉適用於WebForms的Razor語法。但是,如果它適用於一行,並且您可以看到(通過調試)「Request.Form.Item_No」是一個數組(或集合)並且具有值,它看起來好像會起作用。 – 2013-03-02 15:04:46