asp.net和c#问题从添加到gridview的新行保存新数据

时间:2012-03-13 19:50:14

标签: c# asp.net gridview

我遇到了gridviews的问题。基本上我正在编写飞机信息和跟踪系统,并在此过程中了解gridviews的奇迹,但我在向gridview添加新行时遇到了麻烦。当按下按钮时,我能够创建一个新行来输入数据,但是当我点击更新时,我遇到某种回发问题导致数据被遗忘而无法找到解决方法。数据源是一个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#代码

首先,我们有一个从数据库中获取用于构建数据源的信息。它调用对SQL Server 2008数据库的简单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();
}

我的大问题是使用l​​bAddNewCargoDoor和编辑行。当按下更新链接时,它忘记了添加了一个新行,因此只编辑了已存在的行,而不是将新的cargodoor对象插入到数据库中。我只是不确定操作的顺序,以防止这种情况搞砸。

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

尝试更改(请参阅语句顺序的更改)

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

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