我有一个ASP.NET DataTable,我从另一组ASP填充:TextBox字段存储到然后批量插入数据库当我点击添加按钮我得到System.Web.UI.WebControls.TextBox添加到DataTable而不是我在字段中输入的数据,任何人都可以告诉我这里我做错了什么。
守则
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestDataTable._Default" %>
<!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">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<legend>Add New Order
<div>
<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="There is no any data."
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>
<br />
<asp:Table ID="Table1" runat="server" Height="50px" Width="100%"
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"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:TextBox ID="txtInvoiceRef" runat="server"></asp:TextBox ></asp:TableCell>
<asp:TableCell><asp:TextBox ID="txtCustOrderNo" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:TextBox ID="txtProduct" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:TextBox ID="txtQty" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:TextBox ID="txtUnitAmount" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:TextBox ID="txtValue" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:TextBox ID="txtTakenBy" runat="server"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
</fieldset>
</div>
</legend>
</form>
</body>
</html>
背后的守则
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace TestDataTable
{
public partial class _Default : 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();
}
}
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(),
this.txtInvoiceRef.Text.Trim(),
this.txtCustOrderNo.Text.Trim(),
this.txtProduct.Text.Trim(),
this.txtQty.Text.Trim(),
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.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"] = txtAccountRef;
row["InvRef"] = txtInvoiceRef;
row["CustOrderNo"] = txtCustOrderNo;
row["Product"] = txtProduct;
row["Qty"] = txtQty;
row["UnitAmount"] = txtUnitAmount;
row["Value"] = txtValue;
row["TakenBy"] = txtTakenBy;
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;
}
}
}
提前致谢! 贾斯汀
答案 0 :(得分:1)
你忘记了.Text
财产;用它来指定你的控件文本:
row["AccountRef"] = txtAccountRef.Text;
row["InvRef"] = txtInvoiceRef.Text;
row["CustOrderNo"] = txtCustOrderNo.Text;
row["Product"] = txtProduct.Text;
row["Qty"] = txtQty.Text;
row["UnitAmount"] = txtUnitAmount.Text;
row["Value"] = txtValue.Text;
row["TakenBy"] = txtTakenBy.Text;
答案 1 :(得分:0)
您的问题是AddDataToTable()
方法。您可以像这样定义该函数的参数:
string AccountRef
但是然后将值分配给这样的行:
row [“AccountRef”] = txtAccountRef;
看到区别?第二个摘录是在您的页面上使用控件,而不是您传入的参数。您应该将其更改为如下所示:
row [“AccountRef”] = AccountRef;