0
我試圖在單擊鏈接按鈕lnkUpdate
後保存輸入到文本框中的數據。當我點擊lnkupdate
時,似乎數據表沒有變化。(ASP.NET - C#)在將listbox中的textbox.text存儲到數據表時遇到問題
ASPX代碼
<div class="col-md-7">
<div class="well well-sm">
<fieldset>
<legend>Warehouse</legend>
<div class="container-fluid">
<div class="panel panel-default">
<table class="table table-bordered table-hover table-responsive">
<thead>
<tr>
<td><strong>#</strong></td>
<td><strong>BranchName</strong></td>
<td><strong>Quantity</strong></td>
<td><strong>Critical Level</strong></td>
<td></td>
</tr>
</thead>
<tbody>
<asp:ListView ID="lvWarehouse" runat="server"
onitemediting="lvWarehouse_ItemEditing"
onitemcanceling="lvWarehouse_ItemCanceling"
onitemupdating="lvWarehouse_ItemUpdating">
<ItemTemplate>
<tr>
<td><%# Eval("BranchID") %></td>
<td><%# Eval("BranchName") %></td>
<td><%# Eval("Quantity") %></td>
<td><%# Eval("CriticalLevel") %></td>
<td>
<asp:LinkButton ID="btnEdit" runat="server" class="glyphicon glyphicon-pencil" CommandName="Edit" />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td><asp:Label ID="lblID" runat="server" Text='<%# Eval("BranchID") %>' /></td>
<td><asp:Label ID="lblBranchName" runat="server" Text='<%# Eval("BranchName") %>' /></td>
<td><asp:TextBox ID="txtQty" runat="server" class="form-control input-sm" Text='<%# Eval("Quantity") %>' /></td>
<td><asp:TextBox ID="txtCritical" runat="server" class="form-control input-sm" Text='<%# Eval("CriticalLevel") %>' /></td>
<td>
<asp:LinkButton ID="lnkUpdate" runat="server" class="glyphicon glyphicon-ok" CommandName="Update" />
<asp:LinkButton ID="lnkCancel" runat="server" class="glyphicon glyphicon-remove" CommandName="Cancel" />
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
</tbody>
</table>
</div>
</div>
</fieldset>
</div>
</div>
的.cs代碼
SqlConnection con = new SqlConnection(Helper.GetCon());
DataTable ProdDetails;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetSuppliers();
GetProdDetails();
}
}
void GetSuppliers()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT SupplierID, SupplierName FROM Suppliers";
SqlDataReader data = cmd.ExecuteReader();
ddlSupplierName.DataSource = data;
ddlSupplierName.DataTextField = "SupplierName";
ddlSupplierName.DataValueField = "SupplierID";
ddlSupplierName.DataBind();
con.Close();
}
void GetProdDetails()
{
ProdDetails = (DataTable)Session["proddetails"];
if (ProdDetails == null)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT b.BranchID, b.BranchName, pd.QtyonHand AS Quantity, " +
"pd.CriticalLevel FROM Branches AS b LEFT JOIN ProductDetails AS pd ON b.BranchID=pd.BranchID";
SqlDataAdapter data = new SqlDataAdapter(cmd);
ProdDetails = new DataTable();
data.Fill(ProdDetails);
DataRow[] rowList = ProdDetails.Select();
foreach (DataRow dr in rowList)
{
dr["Quantity"] = "0";
dr["CriticalLevel"] = "0";
}
lvWarehouse.DataSource = ProdDetails;
lvWarehouse.DataBind();
con.Close();
Session["proddetails"] = ProdDetails;
}
else
{
lvWarehouse.DataSource = ProdDetails;
lvWarehouse.DataBind();
Session["proddetails"] = ProdDetails;
}
TotalQuantity();
}
void TotalQuantity()
{
int total = 0;
for (int nRow = 0; nRow <= ProdDetails.Rows.Count - 1; nRow++)
{
int qty = int.Parse(ProdDetails.Rows[nRow]["Quantity"].ToString());
total = total + qty;
}
txtQuantity.Text = total.ToString(); ;
}
protected void lvWarehouse_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
GetProdDetails();
string lblID = (lvWarehouse.Items[e.ItemIndex].FindControl("lblID") as Label).Text;
TextBox Qty = (TextBox)lvWarehouse.Items[e.ItemIndex].FindControl("txtQty");
string txtQty = (lvWarehouse.Items[e.ItemIndex].FindControl("txtQty") as TextBox).Text;
string txtCritical = (lvWarehouse.Items[e.ItemIndex].FindControl("txtCritical") as TextBox).Text;
foreach (DataRow row in ProdDetails.Rows)
{
if (row["BranchID"].ToString() == lblID)
{
row["Quantity"] = Qty.Text;
row["CriticalLevel"] = txtCritical;
break;
}
}
lvWarehouse.EditIndex = -1;
GetProdDetails();
}
你需要調用AcceptChanges方法在爲循環像這樣 - ** ProdDetails.AcceptChanges()** –
爲什麼要調用GetProDetails()作爲lvWarehous的第一行e_ItemUpdating?當然,您正在重新列表視圖,以便任何輸入的細節將被替換爲原始數據? –
找不到用於保存對數據庫的更改的代碼。 –