2012-10-27 70 views
1


我試圖做一個可編輯GridView,當RowUpdating叫我從TextBoxes得到的值是舊值,而不是新的。Asp.net GridView的RowUpdating返回舊值

我的GridView:

<asp:GridView ID="GVProducts" runat="server" AutoGenerateColumns="False" 
    CellPadding="4" BackColor="White" BorderColor="#3366CC" BorderStyle="None" 
    BorderWidth="1px" DataKeyNames="phoneId" 
    onrowcancelingedit="GVProducts_RowCancelingEdit" 
    onrowdeleting="GVProducts_RowDeleting" 
    onrowediting="GVProducts_RowEditing" onrowupdating="GVProducts_RowUpdating" 
    > 
    <Columns> 
     <asp:CommandField ButtonType="Button" EditText="ערוך" HeaderText="עריכה" 
      InsertText="הוסף" NewText="חדש" SelectText="בחר" ShowEditButton="True" 
      UpdateText="עדכן" CancelText="בטל" DeleteText="מחק" 
      InsertVisible="False" CausesValidation="False" /> 
     <asp:CommandField ButtonType="Button" CancelText="בטל" DeleteText="מחק" 
      EditText="ערוך" InsertText="הוסף" NewText="חדש" SelectText="בחר" 
      ShowDeleteButton="True" UpdateText="עדכן" HeaderText="מחיקה" 
      CausesValidation="False" /> 
     <asp:BoundField DataField="phoneId" HeaderText="מספר מכשיר" ReadOnly="True" 
      SortExpression="phoneId" /> 
     <asp:TemplateField HeaderText="צבע"> 
      <ControlStyle Width="100px" /> 
      <FooterStyle Width="100px" /> 
      <HeaderStyle Width="100px" /> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="חברה"> 
      <ItemTemplate> 
       <asp:Label ID="lblBrand" runat="server"></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="דגם"> 
      <ItemTemplate> 
       <asp:Label ID="lblModel" runat="server"></asp:Label> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="כמות" SortExpression="amount"> 
      <ItemTemplate> 
       <asp:Label ID="Label1" runat="server" Text='<%# Bind("amount") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("amount") %>'></asp:TextBox> 
      </EditItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="כמות מינימלית" SortExpression="minAmount"> 
      <ItemTemplate> 
       <asp:Label ID="Label2" runat="server" Text='<%# Bind("minAmount") %>'></asp:Label> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("minAmount") %>'></asp:TextBox> 
      </EditItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="price" HeaderText="מחיר" SortExpression="price" /> 
     <asp:TemplateField HeaderText="תמונה"> 
      <ItemTemplate> 
       <asp:Image ID="img" runat="server" 
        ImageUrl='<%# DataBinder.Eval(Container.DataItem,"pic","images/{0}") %>' /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" /> 
    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" /> 
    <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" /> 
    <RowStyle BackColor="White" ForeColor="#003399" /> 
    <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" /> 
    <SortedAscendingCellStyle BackColor="#EDF6F6" /> 
    <SortedAscendingHeaderStyle BackColor="#0D4AC4" /> 
    <SortedDescendingCellStyle BackColor="#D6DFDF" /> 
    <SortedDescendingHeaderStyle BackColor="#002876" /> 
</asp:GridView> 

我RowEditing(UpdateGVLabel不相關):

protected void GVProducts_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    GVProducts.EditIndex = e.NewEditIndex; 
    GVProducts.DataSource = products; 

    UpdateGVlabel(); 
    //products = connection.GetData("Select * From Products", "Products"); 
    if (products.Rows.Count > 0) 
    { 
     ((TextBox)GVProducts.Rows[e.NewEditIndex].Cells[6].Controls[1]).Text = products.Rows[e.NewEditIndex][4].ToString(); 
     ((TextBox)GVProducts.Rows[e.NewEditIndex].Cells[7].Controls[1]).Text = products.Rows[e.NewEditIndex][5].ToString(); 
    } 
} 

我RowUpdating(UpdateGVLabel不相關):

protected void GVProducts_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    bool abort = false; 
    int amount = 0, minAmount = 0; 
    int id = int.Parse(GVProducts.Rows[e.RowIndex].Cells[2].Text); 
    try 
    { 
     amount = int.Parse(((TextBox)GVProducts.Rows[e.RowIndex].Cells[6].Controls[1]).Text); 
     minAmount = int.Parse(((TextBox)GVProducts.Rows[e.RowIndex].Cells[7].Controls[1]).Text); 
     if (amount < minAmount) 
     { 
      MessageBox.Show("אין אפשרות להגדיר כמות שקטנה מהכמות המינימלית!"); 
      abort = true; 
     } 
     if (minAmount <= 0) 
     { 
      MessageBox.Show("כמות מינימלית לא יכולה להיות אפס או פחות!\nאם ברצונך לציין שהחנות לא מוכרת את המכשיר הזה, יש ללחוץ על \"מחק\""); 
      abort = true; 
     } 
     if (amount < 0) 
     { 
      MessageBox.Show("כמות לא יכולה להיות שלילית"); 
      abort = true; 
     } 
    } 
    catch (FormatException) 
    { 
     MessageBox.Show("כמויות חייבות להיות מספרים!"); 
     abort = true; 
    } 
    if (!abort) 
    { 
     connection = new Connect(Server.MapPath("App_Data/ado1.mdb")); 
     OleDbCommand cmd = new OleDbCommand(); 
     cmd.CommandText = "Update Products Set amount=" + amount + ", minAmount=" + minAmount + " Where phoneId=" + id; 
     products = connection.GetData("Select * From Products", "Products"); 
     connection.ChangeDatabase(cmd); GVProducts.EditIndex = -1; 
     GVProducts.DataSource = products; 

     UpdateGVlabel(); 

     MessageBox.Show("המכשיר נערך בהצלחה!"); 
    } 
} 

謝謝!

+0

什麼是UpdateGVlabel(); 在你的代碼? –

+0

我有字段應該與背景等顯示,所以這種方法做到這一點。它與我的問題無關 –

+0

你在頁面加載中寫了些什麼?你是否在頁面加載時綁定了gridview?你檢查過isPostback嗎? –

回答

0

寫一行代碼更新

int i=convert.toint32(e.NewValues["Standard"]) 
+0

它返回舊值 –

1

試試這個

protected void GVProducts_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
//your code 

if (!abort) 
    { 
     connection = new Connect(Server.MapPath("App_Data/ado1.mdb")); 
     OleDbCommand cmd = new OleDbCommand(); 
     cmd.CommandText = "Update Products Set amount=" + amount + ", minAmount=" + minAmount + " Where phoneId=" + id; 
     cmd.ExecuteNonQuery();//OR "connection.ChangeDatabase(cmd);" if it updates your database 
     products = connection.GetData("Select * From Products", "Products"); 
     GVProducts.EditIndex = -1; 
     GVProducts.DataSource = products; 
     GVProducts.DataBind(); 
     UpdateGVlabel(); 

     MessageBox.Show("המכשיר נערך בהצלחה!"); 
    } 
} 

的問題是你叫connection.ChangeDatabase(cmd);從表中讀取數據後更新數據庫這樣products = connection.GetData("Select * From Products", "Products");因此你總是有從你的分貝的舊值和你的分貝的值也得到了更新..

還提供此後GVProducts.DataBind(); provid數據源GVProducts.DataSource = products;綁定網格。