asp.net GridView 未正确更新

发布于 2024-09-19 00:11:58 字数 2190 浏览 6 评论 0原文

我有一个带有这些参数的 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();
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苦笑流年记忆 2024-09-26 00:11:58

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

if (!IsPostBack)
{
    Gridview1.Datasource = BLL.SomeClass.Load(SomeRecordId);
    GridView1.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();
}
深海少女心 2024-09-26 00:11:58

RowUpdatingRowUpdated 在不同时间触发。看看这不是你的问题。

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

盛装女皇 2024-09-26 00:11:58

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

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文