我正在使用 C#-4.0 在 Asp.Net 上工作。我想在 GridView 中从最终用户那里获取输入。像
在 DropDownList 或 TextBox 中,我输入一些值并单击“添加”,然后将添加新记录,但以前的数据已丢失。
问题是当我更改控件的值时,它不会保存在它所绑定的 DataTable 中。如何将此值保存到 DataTable。我在这里错过了什么代码?
设计师代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="SaleOrder.aspx.cs" Inherits="Transactions_SaleOrder" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FormHeader" Runat="Server">
<p>Sale Order</p>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="FormBody" Runat="Server">
<table class="style1">
<tr>
<td>
<asp:Label ID="lblDate" runat="server" Text="Date : "></asp:Label>
<asp:TextBox ID="txtSODate" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="txtSODate_CalendarExtender" runat="server"
Enabled="True" TargetControlID="txtSODate" Format="dd/MM/yyyy" PopupButtonID="ImageButton1">
</asp:CalendarExtender>
<asp:MaskedEditExtender ID="txtSODate_MaskedEditExtender" runat="server"
Enabled="True" Mask="99/99/9999" MaskType="Date" TargetControlID="txtSODate">
</asp:MaskedEditExtender>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="~/images/Calendar_scheduleHS.png" />
</td>
</tr>
<tr>
<td>
Shift :
<asp:DropDownList ID="DropDownList1" runat="server" Width="300px">
<asp:ListItem Value="Morning">MORNING</asp:ListItem>
<asp:ListItem Value="EVENING"></asp:ListItem>
<asp:ListItem Value="OTHERS"></asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
onrowcommand="GridView1_RowCommand">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:DropDownList ID="ddlProduct" runat="server" Width="300"
DataSource='<%# dtProductMaster %>'
DataTextField="PDescr"
DataValueField="PID"
>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:TextBox ID="txtQuan" runat="server" MaxLength="5" style="text-align:right" Text='<%# BIND("QUAN") %>'></asp:TextBox>
<asp:MaskedEditExtender ID="txtQuan_MaskedEditExtender" runat="server" Enabled="True"
Mask="99999" TargetControlID="txtQuan">
</asp:MaskedEditExtender>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:ButtonField CommandName="ADD" Text="Add" />
<asp:ButtonField CommandName="DELETE" Text="Delete" />
</Columns>
<EditRowStyle BackColor="#999999" />
<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" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSave" runat="server" Text="Save" Width="58px" />
</td>
</tr>
</table>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="FormFooter" Runat="Server">
</asp:Content>
它背后的 C# 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Transactions_SaleOrder : System.Web.UI.Page
{
internal DataTable dtProductMaster_;
internal DataTable dtProductMaster
{
get
{
if (dtProductMaster_ == null)
{
clsData d = new clsData();
d.Select("select PID, PEDESCR2 + ' ' + PEDESCR3 as PDescr From TBLPROD_MAST");
dtProductMaster_ = d.DataTable;
}
return dtProductMaster_;
}
}
DataTable dtProductDet
{
get
{
if (ViewState["dtProductDet"] != null)
{
return (DataTable)ViewState["dtProductDet"];
}
else
{
return null;
}
}
set
{
ViewState["dtProductDet"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
txtSODate.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
//--
dtProductDet = new DataTable("dtProductDet");
dtProductDet.Columns.Add("PID", typeof(int));
dtProductDet.Columns.Add("PName", typeof(string));
dtProductDet.Columns.Add("Quan", typeof(decimal));
dtProductDet.Rows.Add(0,"",0);
//--
}
GridView1.DataSource = dtProductDet;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName.ToUpper())
{
case "ADD":
dtProductDet.Rows.Add(0, "", 0);
break;
case "DELETE":
dtProductDet.Rows.RemoveAt(Convert.ToInt32(e.CommandArgument));
break;
}
}
}
我怎样才能实现我的目标。