我begineer asp.net開發的, 網格視圖包含的GridView數據數據庫驗證
ProductID, ProductName, Price, Qty, Total
默認設置五列
如果選擇產品名稱,然後價格顯示自動,但數量會被輸入用戶。 如果沒有輸入數量然後顯示消息, 如果任何一列完整填寫保存數據庫?
產品名稱是下拉列表,我需要服務器端代碼 在我的代碼
保護無效btnSave_Click(對象發件人,EventArgs的){
SqlDataAdapter sda;
SqlCommand cmd;
DateTime savedate = DateTime.ParseExact(txtBillDate.Text.Trim() + " " + DateTime.Now.ToString("hh:mm:ss tt"), "dd/MM/yyyy hh:mm:ss tt", null);
TextBox txtProductID, txtPrice, txtQty, txtTotal;
DropDownList ddlProductName;
DataTable mdt = new DataTable();
Label lblGrandTotal;
if (DataCheck())
{
if (txtMobileNumber.Text != "")
{
con.Open();
cmd = new SqlCommand("insert into Billing(BillNumber,BillDate,CustomerName,CustomerMobile) values('" + txtBillNumber.Text + "','" + savedate + "','" + ddlCustomerName.SelectedItem.Text + "','" + txtMobileNumber.Text + "')", con);
for (int i = 0;i< GridView1.Rows.Count ; i++)
{
txtProductID = (TextBox)(GridView1.Rows[i].FindControl("txtProductID"));
ddlProductName = (DropDownList)(GridView1.Rows[i].FindControl("ddlProductName"));
txtPrice = (TextBox)(GridView1.Rows[i].FindControl("txtPrice"));
txtQty = (TextBox)(GridView1.Rows[i].FindControl("txtQty"));
txtTotal = (TextBox)(GridView1.Rows[i].FindControl("txtTotal"));
lblGrandTotal = (Label)(GridView1.Rows[i].FindControl("lblGrandTotal"));
sda = new SqlDataAdapter("insert into BillingChild(ProductID,ProductName,Price,Qty,Total,BillNumber,BillDate,CustomerName,MobileNumber,BillChildNumber) values('" + txtProductID.Text + "','" + ddlProductName.SelectedItem + "','" + Convert.ToDecimal(txtPrice.Text) + "','" + Convert.ToDecimal(txtQty.Text) + "','" + Convert.ToDecimal(txtTotal.Text) + "','" + txtBillNumber.Text + "','" + savedate + "','" + ddlCustomerName.SelectedItem + "','" + txtMobileNumber.Text + "','" + txtBillChildNumber.Text + "')", con);
sda.Fill(mdt);
cmd.ExecuteNonQuery();
}
con.Close();
}
}
else
{
Response.Write("<Script>alert('plz enter Qty')</script>");
}
}
public bool DataCheck()
{
//TextBox txtProductID = null, txtPrice = null, txtQty = null, txtTotal = null;
//DropDownList ddlProductName = null;
//Label lblGrandTotal = null;
TextBox txtProductID, txtPrice, txtQty, txtTotal;
DropDownList ddlProductName;
Label lblGrandTotal;
if (GridView1.Rows.Count != 0)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
txtProductID = (TextBox)(GridView1.Rows[i].FindControl("txtProductID"));
ddlProductName = (DropDownList)(GridView1.Rows[i].FindControl("ddlProductName"));
txtPrice = (TextBox)(GridView1.Rows[i].FindControl("txtPrice"));
txtQty = (TextBox)(GridView1.Rows[i].FindControl("txtQty"));
txtTotal = (TextBox)(GridView1.Rows[i].FindControl("txtTotal"));
lblGrandTotal = (Label)(GridView1.Rows[i].FindControl("lblGrandTotal"));
if (txtQty.Text != "")
{
continue;
}
else
{
return false;
}
}
}
return true;
}
requierfieldvalidator所有txtQty,但依賴於選擇ProductName。我需要在c#中的服務器端代碼.net –
如果您應用所需的字段驗證程序,它將適用於任何產品名稱或其他一些你有沒有嘗試過? – Dotnet
如果您在行(itemtemplates)中有一個帶有輸入字段的gridview併爲其分配驗證程序,則每個元素都會單獨進行驗證,因爲每行都會生成一個元素和驗證程序。 – cbillowes