1

我有一个带有这些参数的 Gridview:

<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"/>
            </Columns>
</asp:GridView>

在更新它调用:

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

它生成更新/编辑/删除按钮,我的删除功能完全按照我想要的方式工作,并且“编辑”按钮生成了应有的可编辑文本框。

我的问题在于更新部分,字符串 Item、Family、Structure 正在获取旧值,而不是我放入生成的文本框中的新值。
如果我在值中硬编码,它们会更新到数据库中,并且DateTime.Now总是在数据库中正确更新,因此更新查询正在工作。

几天来,我一直在看这个/阅读论坛测试的东西。我确定我只是错过了一些我忽略的简单的东西。

谢谢你的帮助。

编辑: 已经回答了,但对于那些好奇的人来说,这是我的 dataBind();

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

3 回答 3

1

RowUpdatingRowUpdated在不同的时间开火。看看是不是你的问题。

于 2010-09-14T19:03:45.680 回答
1

您是否错误地在回发时重新绑定了 GridView?您应该只在初始加载时从数据库中获取数据:

if (!IsPostBack)
{
    Gridview1.Datasource = BLL.SomeClass.Load(SomeRecordId);
    GridView1.DataBind();
}
于 2010-09-14T19:07:06.183 回答
1

尝试使用以下方法获取新值:

//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();
于 2010-09-14T19:11:36.517 回答