1
我有一個包含2項模板1複選框2,文本框這樣一個GridView:如何從GridView中保存項目模板文本數據庫
<asp:GridView
ID="Warehouse_gv"
runat="server"
AllowPaging="True"
align="center"
AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="Warehouse_gv_SqlDS"
onselectedindexchanged="Warehouse_gv_SelectedIndexChanged"
Width="230px" PageSize="700">
<Columns>
<asp:CommandField ShowDeleteButton="False" ShowEditButton="True" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<input id="chkAll"
önclick="javascript:SelectAllCheckboxes(this);"
runat="server" type="checkbox" />
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Line Number"
HeaderText="Line Number" ReadOnly="True"
SortExpression="Line Number" />
<asp:BoundField DataField="Reference"
HeaderText="Reference" ReadOnly="True"
SortExpression="Reference" />
<asp:BoundField DataField="QTY" HeaderText="QTY"
SortExpression="QTY" />
<asp:TemplateField HeaderText="EditQTY">
<ItemTemplate>
<asp:TextBox ID ="test" runat="server" />
</ItemTemplate>
<HeaderTemplate>
EditQTY
</HeaderTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No data could be find!
</EmptyDataTemplate>
</asp:GridView>
這個網格有6列1 editbutton 2複選框(第一項模板)3行否4參考5數量6-editqty(第二項模板)。 但是當我想將數據從這個GridView控件保存到另一個數據庫中,文本框字段(EditQTY列),我把在項目模板字段數據不調,這是我的代碼背後:
protected void SAVE_btn_Click(object sender, EventArgs e)
{
int C = 0;
for (int i = 0; i < Warehouse_gv.Rows.Count; i++)
{
GridViewRow row = Warehouse_gv.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
if (isChecked)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = new SqlConnection(Class1.CnnStr);
cmd.CommandText = "insert into WarehousePart values (@Reference,@EditQty)";
cmd.Connection.Open();
cmd.Parameters.AddWithValue("@Reference", Warehouse_gv.Rows[i].Cells[3].Text);
cmd.Parameters.AddWithValue("@EditQty", Warehouse_gv.Rows[i].Cells[5].Text);
cmd.ExecuteNonQuery();
cmd.Connection.Close();
C = C + 1;
}
}
}.
我用你的代碼,但它的值仍然保存'0'爲EditQTY –
@KatyJ - 我的猜測。你在Page_Load中有數據綁定代碼。請將此代碼封裝在「IsPostBack」塊中。 – adatapost
我發現非常感謝您的幫助!我在if(test!= null)中使用了cmd.Parameters.AddWithValue(「@ OrderQTY」,test.Text),而不是我最後的代碼,它起作用了! –