0

我是 asp.net 开发者的初学者,网格视图包含

ProductID, ProductName, Price, Qty, Total

默认设置在五列

如果选择产品名称则价格自动显示,但数量将输入用户。如果不输入数量则显示消息,如果任何一列完成填写保存数据库?

productName 是下拉列表,我的代码中需要服务器端代码

受保护的无效btnSave_Click(对象发送者,EventArgs e){

    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;

}
4

2 回答 2

0

如果您在所有五列中都使用默认设置,那么您将无法使用 requiredfieldvalidator。尝试正则表达式验证器。

于 2012-11-28T09:48:27.753 回答
0

您可以将 asp.net 必填字段验证器添加到 gridview 中的每个文本框。

<asp:TextBox id="txtQty" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="reqQty" ControlToValidate="txtQty" ErrorMessage="*"></asp:RequiredFieldValidator>

细化

T-SQL

create table products
( 
    id int identity(1,1),
    name varchar(500),
    price decimal(18,2)
)

insert into products values ('Soap', 15.0)
insert into products values ('Provitas', 25.0)
insert into products values ('Paper', 10.0)
insert into products values ('Foam Bath', 150.0)

ASPX

    <asp:GridView ID="gvTest" runat="server" DataSourceID="SqlTest" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField HeaderText="ID" DataField="ID" />
            <asp:BoundField HeaderText="Name" DataField="Name" />
            <asp:BoundField HeaderText="Price" DataField="Price" />
            <asp:TemplateField HeaderText="Qty">
                <ItemTemplate>
                    <asp:TextBox ID="txtQty" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="reqQty" runat="server" ControlToValidate="txtQty" ErrorMessage="*"></asp:RequiredFieldValidator>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlTest" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;Initial Catalog=play;Persist Security Info=True;User ID=user;Password=userpassword" SelectCommand="SELECT id, name, price FROM Products"></asp:SqlDataSource>
    <asp:Button ID="cmdTest" runat="server" Text="Submit" />

C#按钮单击处理程序以获取用户输入的数量

protected void cmdTest_Click(object sender, EventArgs e)
{
    for (int i = 0; i < gvTest.Rows.Count; i++)
    {
        int qty = Convert.ToInt32(((TextBox)gvTest.Rows[i].FindControl("txtQty")).Text);
        // code here
    }
}

验证结果

在此处输入图像描述

于 2012-11-28T09:41:11.277 回答