Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Asp.Net中单击GirdView中的“添加”按钮时,以前的数据丢失_C#_Asp.net_Gridview - Fatal编程技术网

C# 在Asp.Net中单击GirdView中的“添加”按钮时,以前的数据丢失

C# 在Asp.Net中单击GirdView中的“添加”按钮时,以前的数据丢失,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在使用C#-4.0开发Asp.Net。 我想从GridView中的最终用户那里获取输入。像 在DropDownList或文本框中,我输入一些值并单击Add,然后新记录将添加,但以前的数据丢失 问题是,当我更改控件的值时,它不会保存在绑定到的DataTable中。如何将此值保存到DataTable。我错过了什么代码 设计器代码: <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup

我正在使用C#-4.0开发Asp.Net。 我想从GridView中的最终用户那里获取输入。像

在DropDownList或文本框中,我输入一些值并单击Add,然后新记录将添加,但以前的数据丢失

问题是,当我更改控件的值时,它不会保存在绑定到的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>

我怎样才能达到我的目标。

答案已经在前面的评论中给出了。说清楚

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();
}

我找到了解决办法

问题是,当我在DropDownList或TextBox中编辑值时,它的值并没有保存回与GridView和TextBox以及DropDownList绑定的DataTable。 没有自动的方法可以做到这一点,所以我们必须编写手动代码将值保存回DataTable

当我问“添加”按钮在GridViewColumn和“命令”列中时,现在我在GridView页脚中添加了一个按钮

所以,当我点击“添加新行”按钮时,页面被发回,在第一个页面加载事件激发,然后点击按钮事件激发。我在Page.Load事件上添加了代码,若页面被发回,那个么从GridView中选取数据并将其保存到DataTable

Nextly Button\U click事件触发,这样在DataTable中添加一个新行,DataTable将分配给GridView的DataSource,DataBind方法将执行并重新生成GridView

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);
        }
    }

}
我有一个DropDownList,它也绑定到GridView以外的另一个DataTable,因此在DataBind()之后,我必须根据GridView的DataTable中保存的数据手动选择DropDownList中的值

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);
        }
    }

}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!Page.IsPostBack)
{
txtSODate.Text=DateTime.Now.Date.ToString(“dd/MM/yyyy”);
//--
dtProductDet=新数据表(“dtProductDet”);
添加(“PID”,typeof(int));
添加(“PName”,typeof(string));
添加(“权”,类型(十进制));
添加(0,“,0);
//--
SetGridViewSource();
}
其他的
{
数据表dtPD=dtProductDet;
DropDownList ddlProd=null;
如果(GridView1.Rows.Count>0)
{
对于(int i=0;i
看起来您正在回发时重新绑定gridview。您应该将最后两行放入
!IsPostback
block。我尝试了,但是现在没有添加新行。在数据添加到数据源后,需要在单击处理程序中重新绑定<代码>页面加载在处理程序之前被调用。但是输入的数据没有保存回DataTable,我在RowCommand事件中检查了数据表,DataRow为空。它不工作,尽管下次页面加载时,网格中没有显示任何记录。到底什么不工作?当单击“添加”按钮时,当单击“添加”按钮时,是否添加到DATATATE中的空白行中。如果将Page加载事件返回到原来的方式,然后再次尝试,GRIDVIEW1应该是可见的。当您单击add按钮时,您的RowCommand事件将向DataTable中添加一个空行,当回发完成时,您应该在GirdView1中看到这一点-这是否发生了?是的,如果我将databind放在一边,它就发生了!Page.IsPostBack块