簡體   English   中英

c#訂單處理ASP.NET頁

[英]c# Order Processing ASP.NET Page

我正在構建一個ASP.NET頁面,這是一個銷售訂單處理表單,由於我缺乏使用該語言的經驗,我遇到了兩個最后的問題,希望您能為您提供幫助。

問題1-我有一個字段<asp:DropDownList runat="server" ID="txtProduct"></asp:DropDownList>從數據庫表Called Products中提取來填充其選項列表。 我需要根據產品的選擇自動拉出SalesPrice值並將其插入到txtUnitAmount字段中。

問題2-表單填寫完畢並且DataTable中有記錄后,我需要在用戶單擊按鈕時將所有行插入數據庫表SalesOrders中。

我的ASPX代碼(完整)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SOPOrderEntry.aspx.cs" Inherits="CRM2Sage.SOPOrderEntry1" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link rel="Stylesheet" type="text/css" href="stylesheet.css" />
    <title>CRM2Sage - SOP Entry Form</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
            float: left;
            border-style: solid;
            border-width: 1px;
        }
    </style>
    <script type="text/javascript">
        function q_multi_p(form) {
            q = eval(form.txtQty.value)
            p = eval(form.txtUnitAmount.value)
            t = q * p
            form.txtValue.value = t
        }
    </script>
</head>
<body style="padding: 0; margin: 0;">
<div style="width: 1170px; margin: 0 auto;">
    <form id="form1" runat="server">
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:CRM2Sage %>"
    SelectCommand="SELECT [AccountRef], [Name], [Add1], [Add2], [Add3], [Add4], [Add5], [ContactName], [Telephone], [Terms] FROM [Customers]">
    <SelectParameters>
        <asp:Parameter DefaultValue="1" Name="AccountRef" Type="String" />
    </SelectParameters>
    </asp:SqlDataSource> 
    <div style="width: 1170px; height: 211px;">
        <table align="right" cellpadding="0" class="style1">
            <tr>
                <td>
    <asp:DetailsView ID="DetailsView1" runat="server" Height="100%" Width="585px" 
        AutoGenerateRows="False" DataSourceID="SqlDataSource1" 
        EnableModelValidation="True" HorizontalAlign="Left" CellPadding="4" ForeColor="#333333" 
                        GridLines="None">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
        <EditRowStyle BackColor="#999999" />
        <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
        <Fields>
            <asp:BoundField DataField="AccountRef" HeaderText="Account Ref" 
                SortExpression="AccountRef" />
            <asp:BoundField DataField="Name" HeaderText="Company Name" 
                SortExpression="Name" />
            <asp:BoundField DataField="Add1" HeaderText="Address" SortExpression="Add1" />
            <asp:BoundField DataField="Add2" SortExpression="Add2" />
            <asp:BoundField DataField="Add3" HeaderText="City" SortExpression="Add3" />
            <asp:BoundField DataField="Add4" HeaderText="County" SortExpression="Add4" />
            <asp:BoundField DataField="Add5" HeaderText="Post Code" SortExpression="Add5" />
            <asp:BoundField DataField="ContactName" HeaderText="Contact" 
                SortExpression="ContactName" />
            <asp:BoundField DataField="Telephone" HeaderText="Telephone" 
                SortExpression="Telephone" />
            <asp:BoundField DataField="Terms" HeaderText="Credit Terms" 
                SortExpression="Terms">
            <HeaderStyle CssClass="lbl" />
            <ItemStyle CssClass="row" />
            </asp:BoundField>
        </Fields>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    </asp:DetailsView>
                </td>
                <td>
                    <table>
                    <tr>
                    <td><asp:Label ID="OrderNoLabel" runat="server" Text="Customer Order No."></asp:Label></td>
                    <td><asp:TextBox ID="OrderNoText" runat="server" 
                            ontextchanged="OrderNoText_TextChanged"></asp:TextBox></td>
                    </tr>
                    <tr>
                    <td><asp:Button ID="GenInvRef" runat="server" Text="Gen Invoice Ref" 
                            onclick="GenInvRef_Click" /></td>
                    <td><asp:TextBox ID="InvRefText" runat="server" 
                            ontextchanged="InvRefText_TextChanged"></asp:TextBox></td>
                    </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    <div style="width: 100%; border: 2px Groved #666; background-color: #FFF; height: 100%; padding: 15px;">
    <fieldset style="height:200px;">
    <asp:Label ID="lblTips" runat="server" ForeColor="White" BackColor="Red" Width="100%" />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            Width="100%" CellPadding="4" EmptyDataText="No Order Lines Created Yet!" 
            ForeColor="#333333" EnableModelValidation="True" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID" Visible="False" />
                <asp:BoundField DataField="AccountRef" HeaderText="Account #" />
                <asp:BoundField DataField="InvRef" HeaderText="Invoice Ref" />
                <asp:BoundField DataField="CustOrderNo" HeaderText="Order Number" />
                <asp:BoundField DataField="Product" HeaderText="Product" />
                <asp:BoundField DataField="Qty" HeaderText="Qty" />
                <asp:BoundField DataField="UnitAmount" HeaderText="Unit Amount" />
                <asp:BoundField DataField="Value" HeaderText="Line Total" />
                <asp:BoundField DataField="TakenBy" HeaderText="Taken By" />
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
            <RowStyle ForeColor="#333333" BackColor="#F7F6F3" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
        <div style="margin: 0 auto; position: relative;">  
            <asp:Table ID="Table1" runat="server" Width="1158px" 
                ForeColor="#333333" GridLines="None">
            <asp:TableHeaderRow EnableTheming="true" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White">
                <asp:TableHeaderCell>Account Ref:</asp:TableHeaderCell>
                <asp:TableHeaderCell>Invoice Ref</asp:TableHeaderCell>
                <asp:TableHeaderCell>Customer Order #</asp:TableHeaderCell>
                <asp:TableHeaderCell>Product</asp:TableHeaderCell>
                <asp:TableHeaderCell>Qty</asp:TableHeaderCell>
                <asp:TableHeaderCell>Unit Amount</asp:TableHeaderCell>
                <asp:TableHeaderCell>Value</asp:TableHeaderCell>
                <asp:TableHeaderCell>Taken By</asp:TableHeaderCell>
            </asp:TableHeaderRow>
            <asp:TableRow ForeColor="#333333" BackColor="#F7F6F3">
                <asp:TableCell><asp:TextBox ID="txtAccountRef" runat="server" CssClass="ContactSmallTextField" ReadOnly="True"></asp:TextBox></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtInvoiceRef" runat="server" CssClass="ContactSmallTextField"></asp:TextBox ></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtCustOrderNo" runat="server" CssClass="ContactSmallTextField"></asp:TextBox></asp:TableCell>
                <asp:TableCell><asp:DropDownList runat="server" ID="txtProduct"></asp:DropDownList></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtQty" runat="server" CssClass="ContactExtTextField" onChange="javascript:q_multi_p(this.form)"></asp:TextBox></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtUnitAmount" runat="server" CssClass="ContactSmallTextField"></asp:TextBox></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtValue" runat="server" CssClass="ContactSmallTextField"></asp:TextBox></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtTakenBy" runat="server" CssClass="ContactSmallTextField"></asp:TextBox></asp:TableCell>           
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell><asp:Button ID="btnAdd" runat="server" Text="Add Order Line" OnClick="btnAdd_Click" CssClass="btn" /></asp:TableCell>
            </asp:TableRow>
            </asp:Table>
        </div>
    </fieldset>
    </div>
    </form>
</div>
</body>
</html>

我的CS代碼完整

using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.Security;
using System.Web.Configuration;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace CRM2Sage
{
    public partial class SOPOrderEntry1 : System.Web.UI.Page
    {

        internal DataTable myDt = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                myDt = new DataTable();
                myDt = CreateDataTable();
                Session["myDatatable"] = myDt;

                this.GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
                this.GridView1.DataBind();

            }
            getAccountRef();
            Fill1();
        }



        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtAccountRef.Text.Trim() == "")
            {
                this.lblTips.Text = "You must have an Account Number.";
                return;
            }
            else
            {
                AddDataToTable (
                    this.txtAccountRef.Text.Trim(),
                    (string)Request.Form["InvRefText"].ToString(),
                    (string)Request.Form["OrderNoText"].ToString(),
                    (string)Request.Form["txtProduct"].ToString(),
                    //this.txtProduct.Text.Trim(),
                    this.txtQty.Text.Trim(),
                    (string)Request.Form["txtUnitAmount"].ToString(),
                    //this.txtUnitAmount.Text.Trim(),
                    this.txtValue.Text.Trim(),
                    this.txtTakenBy.Text.Trim(),
                    (DataTable)Session["myDatatable"]
                );


                this.GridView1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
                this.GridView1.DataBind();

                //this.txtAccountRef.Text = "";
                this.txtInvoiceRef.Text = "";
                //this.txtCustOrderNo.Text = "";
                //this.txtProduct.SelectedItem.Text = "";
                this.txtQty.Text = "";
                //this.txtUnitAmount.Text = "";
                this.txtValue.Text = "";
                this.txtTakenBy.Text = "";
                this.lblTips.Text = "";
            }
        }

        private void AddDataToTable(string AccountRef, string InvRef, string CustOrderNo, string Product, string Qty, string UnitAmount, string Value, string TakenBy, DataTable myTable)
        {
            DataRow row;

            row = myTable.NewRow();

            row["id"] = Guid.NewGuid().ToString();
            row["AccountRef"] = AccountRef;
            row["InvRef"] = InvRef;
            row["CustOrderNo"] = CustOrderNo;
            row["Product"] = Product;
            row["Qty"] = Qty;
            row["UnitAmount"] = UnitAmount;
            row["Value"] = Value;
            row["TakenBy"] = txtTakenBy.Text;


            myTable.Rows.Add(row);
        }

        private DataTable CreateDataTable()
        {
            DataTable myDataTable = new DataTable();

            DataColumn myDataColumn;

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "id";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "AccountRef";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "InvRef";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "CustOrderNo";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "Product";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "Qty";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "UnitAmount";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "Value";
            myDataTable.Columns.Add(myDataColumn);

            myDataColumn = new DataColumn();
            myDataColumn.DataType = Type.GetType("System.String");
            myDataColumn.ColumnName = "TakenBy";
            myDataTable.Columns.Add(myDataColumn);

            return myDataTable;
        }

        public void Fill1()
        {
            string connectionString = WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;
            using (SqlConnection _con = new SqlConnection(connectionString))
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM Products", _con))
            {
                cmd.Connection.Open();

                SqlDataReader ddlValues;
                ddlValues = cmd.ExecuteReader();

                txtProduct.DataSource = ddlValues;
                txtProduct.DataValueField = "Description";
                txtProduct.DataTextField = "Description";
                txtProduct.DataBind();

                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }
        }

        public void getAccountRef()
        {
            string getAccountRef = (string)Request.QueryString["AccountRef"].ToString();
            SqlDataSource1.SelectParameters[0].DefaultValue = getAccountRef;
            txtAccountRef.Text = getAccountRef;
        }

        public void getOrderNo()
        {
            string getOrderNo = (string)Request.Form["OrderNoText"].ToString();
            txtCustOrderNo.Text = getOrderNo;
        }

        protected void OrderNoText_TextChanged(object sender, EventArgs e)
        {
            getOrderNo();
        }

        private string GenerateId()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
            {
                i *= ((int)b + 1);
            }
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }

        protected void GenInvRef_Click(object sender, EventArgs e)
        {
            string GenInvRef = GenerateId();
            InvRefText.Text = GenInvRef;
        }

        public void getInvRef()
        {
            string getInvRef = (string)Request.Form["InvRefText"].ToString();
            txtInvoiceRef.Text = getInvRef;
        }

        protected void InvRefText_TextChanged(object sender, EventArgs e)
        {
            getInvRef();
        }

        //public void Fill2()
        //{
        //    string getProdValue = WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;
        //    using (SqlConnection _con = new SqlConnection(getProdValue))
        //    using (SqlCommand cmd = new SqlCommand("SELECT SalesPrice FROM Products WHERE Description = '" + txtProduct + "'", _con))
        //    {
        //        cmd.Connection.Open();
        //        var unitPrice = cmd.ExecuteScalar();
        //        txtUnitAmount.Text = unitPrice.ToString();
        //        cmd.Connection.Close();
        //        cmd.Connection.Dispose();
        //    }           
        //}

        //protected void txtProduct_OnSelectedIndexChanged(object sender, EventArgs e)
        //{
        //    Fill2();
        //}

    }
}

我想我將在一定的幫助和指導下完成,並且非常感激,這也將使我為進一步的項目做好准備,在此先感謝所有回答的人,我當然會為所有幫助的人提供全面的支持。

同時,感謝@​​ Slappy,@ Nick和@MikeEast以及到目前為止對其他方面確實有幫助的其他人。

最好

賈斯汀

由於我無法編寫完整的代碼,因此只給您一些提示。

 1. Create a class with the field name you want to insert into you DB. 
    Like for Sales order, whatever fields you have in your form, 
    create properties for the same.

 2. Bind the grid and Dropdowns from the database using the datatable.

 3. Put a hidden field on the form to detect insert or update process

 4. On selection chnage of grid or dropdown fill the form fields, 
    you have for sales order. By extracting the rows from the datatable 
    and fill it to textboxes.

 5. While save button click , create a method that will be used for filling
    up the SalesOrder Class object. pass this object to data layer, extract 
   the properties and pass it as parameter in your AddUpadte query.

您正在使用哪個框架? 應當設置下拉列表auto postback = true,然后創建事件SelectedIndex Change,然后編寫代碼以自動填充文本帖子( textboxName.text = dropdownlistName.selectedItem.Tex )。

希望能幫助到你:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM