2012-03-13 59 views
2

我有一个GridView的问题。基本上我正在编写一个飞机信息和跟踪系统,并在此过程中了解gridview的奇妙之处,但是我无法在gridview中添加新行。我可以创建一个新行来输入数据时,按下按钮,但当我点击更新,我有某种回发问题,导致数据被遗忘,无法找到一种方法。数据源是包含双数和字符串的cargodoor对象列表,包含测量值,名称等。这里是我的代码。asp.net和c#的问题保存从新行添加到gridview的新数据

<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数据库调用SQL Server 2008数据库并返回一个飞机对象中的数据,其中包含一个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对象插入到数据库中。我只是不知道为什么要防止这种情况发生。

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

回答

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

请注意,我不能添加评论(由于较少的声誉)和我知道,这应该成为评论。

相关问题