开发者

asp.net GridView isn't updating properly

I have a Gridview with these parameters:

<asp:GridView runat="server" ID="ItemGrid" CssClass="Grid"
                AutoGenerateColumns="false"
                AutoGenerateDeleteButton="true" OnRowDeleting="RowDeleting"
                AutoGenerateEditButton="true" onRowEditing="RowEdit" 
                OnRowCancelingEdit="CancelRowEdit" onRowUpdating="RowUpdating"
                DataKeyNames="Item_ID">
            <Columns>
                <asp:BoundField HeaderText="Item" DataField="Item"/>
                <asp:BoundField HeaderText="Family" DataField="Family"/>
                <asp:BoundField HeaderText="Structure" DataField="Structure"/>
                <asp:BoundField HeaderText="Updated" ReadOnly="true" DataFormatString="{0:d}" DataField="Updated"/>
  开发者_JAVA技巧          </Columns>
</asp:GridView>

On updating it calls:

protected void RowUpdating(object sender, GridViewUpdateEventArgs e){
    int Item_ID = (int)this.ItemGrid.DataKeys[e.RowIndex][0];
//Problem is something right here:
    string Item = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
    string Family = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
    string Structure = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[3].Controls[0]).Text;

    ItemTableAdapter taItem = new ItemTableAdapter();
    taItem.UpdateItem(Item, Family, Structure, DateTime.Now, Item_ID);
    //just a <asp:Label> for seeing some output.
    Alert.Text= string.Format("Item:{0}Family:{1}Structure:{2}",Item,Family,Structure);

    this.ItemGrid.EditIndex = -1;
    dataBind();        
}

It generates the Update/Edit/Delete buttons, my Delete function is working exactly how I want and the 'Edit' button generates editable TextBoxes as it should.

My problem is in the updating part, the strings Item, Family, Structure are getting the old values, not the new values I put in the generated text boxes.

If I hard code in values they are updated to the database and the DateTime.Now is always updating correctly in the database so the update query is working.

I've been looking at this/reading forums testing things for a couple days now. I'm sure I'm just missing something simple that I have overlooked.

Thanks for any help.

Edit: It has been answered but for those who were curious this is my dataBind();

protected void dataBind()
{
    ItemTableAdapter taItem = new ItemTableAdapter();
    this.ItemGrid.DataSource = taItem.GetActive();
    this.ItemGrid.DataBind();
}


Are you re-binding your GridView on postback by mistake? You should only fetch the data from the database on initial load:

if (!IsPostBack)
{
    Gridview1.Datasource = BLL.SomeClass.Load(SomeRecordId);
    GridView1.DataBind();
}


RowUpdating and RowUpdated fire at different times. See if that isn't your problem.


Try using the following method to get your new values:

//string Item = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
//string Family = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
//string Structure = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[3].Controls[0]).Text;

string Item = e.NewValues["Item"].ToString();
string Family = e.NewValues["Family"].ToString();
string Structure = e.NewValues["Structure"].ToString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜