我正在構建一個ASP.NET頁面,這是一個銷售訂單處理表單我有兩個最後的問題,由於我的語言經驗不足,我希望你能幫上忙。c#訂單處理ASP.NET頁面
問題1 - 我有一個字段<asp:DropDownList runat="server" ID="txtProduct"></asp:DropDownList>
,它從數據庫表「調用產品」中拉取以填充其選項列表。我需要自動提取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和其他許多迄今爲止真正有用的人。
最佳
賈斯汀
@AmRan感謝您的這一點,你可能會發布你指的類的例子嗎? – 2011-03-31 08:47:03
一般情況我所做的是我使用Class來分別從數據庫中獲取和設置數據。我通常避免數據表。使用數據表格進行網格和下拉菜單中的綁定。如果網格是可編輯的,那麼我更喜歡BindingList。無論如何。給我時間我會上傳一個示例應用程序來使用類來向數據庫和從數據庫中填充數據。 –
2011-03-31 08:52:42
@AmRan我可以使用您的服務一個小時來幫助我完成這項工作嗎?是如此,請給我發電子郵件[email protected] – 2011-03-31 08:58:07