asp.net和c#在保存添加到网格视图的新行中的新数据时出现问题

本文关键字:数据 新数据 问题 新行中 asp 保存 添加 视图 网格 net | 更新日期: 2023-09-27 18:27:36

我在网格视图方面遇到问题。基本上,我正在编写一个飞机信息和跟踪系统,并在这个过程中了解网格视图的奇妙之处,但我很难在网格视图中添加新行。当按下按钮时,我可以创建一个新行来输入数据,但当我单击更新时,我遇到了某种回发问题,导致数据被遗忘,无法找到解决方法。数据源是cargodoor对象的列表,其中包含双精度和包含测量值、名称等的字符串。这是我的代码。

<asp:UpdatePanel ChildrenAsTriggers="true" ID="UpdatePanel2" runat="server">
<ContentTemplate>
<div id="divAddCargoDoor" style="width:100%" runat="server" class="AlignRight">
    <asp:LinkButton ID="lbAddNewCargoDoor" runat="server"
        Text="<%$ Resources:ls, AddCargoDoor %>" 
        OnClick="lbAddNewCargoDoor_Click"></asp:LinkButton><br /><br />
</div>
<div id="divCargoDoorNoDoors" runat="server">
    <asp:Label ID="lCargoDoorNoDoors" runat="server"
        Text="<%$ Resources:ls, NoCargoDoors %>"></asp:Label>
</div>
<asp:GridView ID="gvCargoDoors" runat="server" AutoGenerateColumns="False"
   DataKeyNames="Id" Width="100%" 
       OnRowEditing="gvCargoDoors_RowEditing"
       OnRowUpdating="gvCargoDoors_RowUpdating"
       OnRowDeleting="gvCargoDoors_RowDeleting"
       OnRowCancelingEdit="gvCargoDoors_RowCancellingEdit">
    <Columns>
        <asp:CommandField AccessibleHeaderText="Edit" ShowEditButton="True" />
        <asp:TemplateField AccessibleHeaderText="Name"
             HeaderText="<%$ Resources:ls, Description %>">
            <ItemTemplate>
                <asp:Label ID="lName" runat="server" Text='<%# Bind("Name") %>' />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>' /> 
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField AccessibleHeaderText="Width"
            HeaderText="<%$ Resources:ls, Width %>">
            <ItemTemplate>
                <asp:Label ID="lWidth" runat="server" Text='<%# Bind("Width") %>' />
            </ItemTemplate>
            <EditItemTemplate>
               <asp:TextBox ID="CDWidth" runat="server" Text='<%# Bind("Width") %>' /> 
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField AccessibleHeaderText="Height" 
            HeaderText="<%$ Resources:ls, Height %>">
            <ItemTemplate>
                <asp:Label ID="lHeight" runat="server" Text='<%# Bind("Height") %>' />
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox ID="CDHeight" runat="server" Text='<%# Bind("Height") %>' />
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:CommandField AccessibleHeaderText="Delete" ShowDeleteButton="True"
            DeleteText="X" />
    </Columns>
</asp:GridView>
<br />
</ContentTemplate>
</asp:UpdatePanel>

现在针对C#代码

首先,我们有一个片段,它从用于构建数据源的数据库中获取信息。它对SQLServer2008数据库调用一个简单的sql数据库调用,并返回飞机对象中的数据,该对象包含cargodoor对象列表。

private void BuildDataSource()
    {
        this.ac = Charter.Aircraft.Retrieve(Convert.ToInt32(this.hfAircraftID.Value));
    }

现在这是实际货舱门控制的代码

private void BindCargoDoors()
{
    if (ac.CargoDoors.Count == 0)
    {
        //display a label telling user there are no cargo doors
        divCargoDoorNoDoors.Visible = true;
    }
    else
    {
        //bind the cargodoor object list to the gridview
        divCargoDoorNoDoors.Visible = false;
        this.gvCargoDoors.DataSource = this.ac.CargoDoors;
        this.gvCargoDoors.DataBind();
    }
}
protected void gvCargoDoors_RowEditing(object sender, GridViewEditEventArgs e)
{
    //get the index of the row and enter row editing mode
    this.gvCargoDoors.EditIndex = e.NewEditIndex;
    BuildDataSource();
    BindCargoDoors();
}
protected void gvCargoDoors_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //get the row being edited
    GridViewRow row = this.gvCargoDoors.Rows[e.RowIndex];
    //create a new cargo door to store the info in
    CargoDoor cd = new CargoDoor();
    //Retrieve the id of the cargodoor
    cd.Id = Convert.ToInt32(gvCargoDoors.DataKeys[e.RowIndex].Values["Id"]);
    if (cd.Id == 0)    //will apply when new cargodoor is added
    {
        //fill in the cargodoor object from data in the row
        cd.DateAdded = DateTime.Now;
        cd.DateModified = DateTime.Now;
        cd.Name = ((TextBox)row.FindControl("Name")).Text;
        cd.Width = Convert.ToDouble(((TextBox)row.FindControl("Width")).Text);
        cd.Height = Convert.ToDouble(((TextBox)row.FindControl("Height")).Text);
        cd.Owner = this.ac.Owner;
        cd.AircraftId = this.ac.Id;
        //insert the new cargodoor into the database
        Charter.Aircraft.InsertCargoDoor(cd);
    }
    else
    {
        //Retrieve old cargodoor info and fill in object info into cd
        CargoDoor cdOrig = Charter.Aircraft.RetrieveCargoDoorByID(cd.Id);
        //fill in the cargodoor object with retrieved info
        cd.AircraftId = cdOrig.AircraftId;
        cd.DateAdded = cdOrig.DateAdded;
        cd.Notes = cdOrig.Notes;
        cd.Owner = cdOrig.Owner;
        //fill in updated information
        cd.Name = ((TextBox)row.FindControl("Name")).Text;
        cd.Width = Convert.ToInt32(((TextBox)row.FindControl("Width")).Text);
        cd.Height = Convert.ToInt32(((TextBox)row.FindControl("Height")).Text);
        cd.DateModified = DateTime.Now;
        //save the new cargodoor info
        Charter.Aircraft.UpdateCargoDoor(cd);
    }
    //rebuild data source to get new cargo door
    BuildDataSource();
    //Reset the edit index.
    this.gvCargoDoors.EditIndex = -1;
    //Bind data to the GridView control.
    BindCargoDoors();
}
protected void gvCargoDoors_RowCancellingEdit(object sender, 
    GridViewCancelEditEventArgs e)
{
    this.gvCargoDoors.EditIndex = -1;
    BuildDataSource();
    BindCargoDoors();
}
protected void gvCargoDoors_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    BuildDataSource();
    int id = Convert.ToInt32(gvCargoDoors.DataKeys[e.RowIndex].Values["Id"]);
    Charter.Aircraft.DeleteCargoDoor(id);
    BuildDataSource();
    BindCargoDoors();
}
protected void lbAddNewCargoDoor_Click(object sender, EventArgs e)
{
    //trigger the edit mode with an edit row index of 0
    this.gvCargoDoors.SetEditRow(0);
    //insert a new cargodoor into the source object
    this.ac.CargoDoors.Insert(0, new CargoDoor());
    //bind the data
    this.gvCargoDoors.DataSource = this.ac.CargoDoors;
    BindCargoDoors();
}

我最大的问题是lbAddNewCargoDoor和一行的编辑。当按下更新链接时,它忘记了添加了新行,因此只编辑已经存在的行,而不是将新的cargodoor对象插入数据库。我只是不确定防止这件事搞砸的行动顺序。

如有任何帮助,我们将不胜感激。谢谢

asp.net和c#在保存添加到网格视图的新行中的新数据时出现问题

尝试更改(按语句顺序查看更改)

protected void lbAddNewCargoDoor_Click(object sender, EventArgs e)
{
    //trigger the edit mode with an edit row index of 0
    this.gvCargoDoors.SetEditRow(0);
    //insert a new cargodoor into the source object
    this.ac.CargoDoors.Insert(0, new CargoDoor());
    //bind the data
    this.gvCargoDoors.DataSource = this.ac.CargoDoors;
    BindCargoDoors();
}

protected void lbAddNewCargoDoor_Click(object sender, EventArgs e)
{
    //insert a new cargodoor into the source object
    this.ac.CargoDoors.Insert(0, new CargoDoor());
    //bind the data
    this.gvCargoDoors.DataSource = this.ac.CargoDoors;
    //trigger the edit mode with an edit row index of 0
    this.gvCargoDoors.SetEditRow(0);
    BindCargoDoors();
}

请注意,我无法添加评论(由于声誉较低),我知道这应该是一条评论。