2013-04-08 57 views
0

我正在與C#-4.0 Asp.Net。 我想從GridView中的最終用戶獲取輸入。像以前的數據lossted當點擊添加按鈕在GirdView Asp.Net

enter image description here

在DropDownList中或在文本框中輸入我的加入則新的記錄將增加,但以前的數據丟失了一些價值和點擊。

enter image description here

的問題是當我改變的控制值時,它不保存在數據表中其所綁定。我如何將此值保存到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; 
     } 
    } 
} 

我如何能實現我的目標。

+0

看起來你正在rebbackding回發gridview。你應該把最後兩行寫入'!IsPostback'塊。 – 2013-04-08 12:58:31

+0

我試過了,但是現在不添加新行。 – 2013-04-08 13:00:26

+0

在數據添加到數據源之後,您需要在點擊處理程序中重新綁定。在處理程序之前調用「Page_Load」。 – 2013-04-08 13:02:17

回答

0

我已經得到了解決方案。

問題是,當我在DropDownList或TextBox中編輯值時,它的值不會保存回與GridView和TextBox和DropDownList綁定的DataTable。 沒有自動執行此操作的方法,因此我們必須編寫手動代碼以將值保存回DataTable。

當我問的問題添加按鈕是在GridViewColumn,它是命令列,現在我已經添加了一個按鈕在GridView頁腳。

所以,當我點擊「添加新行」按鈕,頁面被回傳,在第一頁加載事件觸發,然後Button_Click事件觸發。我在Page.Load事件上添加了代碼,如果頁面被回發,然後從GridView中選取數據並將其保存到DataTable中。

接下來Button_click事件觸發,所以一個新行添加在DataTable中,而DataTable將在GridView的DataSource中分配,並且DataBind方法執行並重新生成GridView。我有一個DropDownList它也綁定到一個不同的DataView的GridView,以便DataBind()後,我必須手動選擇DropDownList中的值按照保存在GridView的DataTable中。

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); 
     //-- 
     SetGridViewSource(); 
    } 
    else 
    { 
     DataTable dtPD = dtProductDet; 
     DropDownList ddlProd = null; 
     if (GridView1.Rows.Count > 0) 
     { 
      for (int i = 0; i < GridView1.Rows.Count; i++) 
      { 
       ddlProd = (DropDownList)GridView1.Rows[i].Cells[gvci_Prod].FindControl("ddlProduct"); 
       dtPD.Rows[i]["PID"] = Convert.ToInt32(ddlProd.SelectedItem.Value); 
       dtPD.Rows[i]["PName"] = ddlProd.SelectedItem.Text; 
       dtPD.Rows[i]["QUAN"] = ((TextBox)GridView1.Rows[i].Cells[gvci_Quan].FindControl("txtQuan")).Text; 
      } 
     } 
     dtProductDet = dtPD; 
    } 
} 


protected void btnAdd_Click(object sender, EventArgs e) 
{ 
    dtProductDet.Rows.Add(0, "", 0); 

    DataTable dtPDet = dtProductDet; 
    GridView1.DataSource = dtPDet; 
    GridView1.DataBind(); 


    DropDownList ddlProd = null; 
    DataRow drFind = null; 
    for (int i = 0; i < GridView1.Rows.Count; i++) 
    { 
     ddlProd = (DropDownList)GridView1.Rows[i].Cells[gvci_Prod].FindControl("ddlProduct"); 
     drFind = dtProductMaster.Rows.Find(dtPDet.Rows[i]["PID"]); 
     if(drFind != null) 
     { 
      ddlProd.SelectedIndex = dtProductMaster.Rows.IndexOf(drFind); 
     } 
    } 

} 
0

答案已經在之前的評論中給出。拼出來...

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; 
    } 
    GridView1.DataBind(); 
} 
+0

它不起作用,雖然下次頁面加載時,沒有記錄顯示在網格中 – 2013-04-09 05:40:24

+0

什麼是不工作?單擊添加按鈕時,是否將空行添加到DataTable中? – FastGeek 2013-04-09 10:41:05

+0

當點擊添加按鈕時,Gridview不會顯示在頁面上。 – 2013-04-09 11:37:42