簡體   English   中英

asp.net和c#問題從新行保存新數據添加到gridview

[英]asp.net and c# problems saving new data from new row added to a gridview

我在gridviews中遇到問題。 基本上,我正在編寫飛機信息和跟蹤系統,並且在此過程中了解了GridView的奇妙之處,但是在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 Server 2008數據庫的簡單sql數據庫調用,並在飛機對象(包含貨艙門對象列表)中返回數據。

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對象。 我只是不確定操作順序,以防止這種情況惡化。

任何幫助,將不勝感激。 謝謝。

嘗試更改(請參閱語句順序的更改)

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

請注意,由於聲譽下降,我無法添加評論,並且我知道這應該是評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM