更新GridView时出现问题

编程入门 行业动态 更新时间:2024-10-18 10:19:18
本文介绍了更新GridView时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" DataKeyNames="ID" onrowcancelingedit="GridView1_RowCancelingEdit"> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <Columns> <asp:TemplateField HeaderText="Name"> <EditItemTemplate> <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Age"> <EditItemTemplate> <asp:TextBox ID="txtAge" runat="server" Text='<%# Eval("Age") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblAge" runat="server" Text='<%# Eval("Age") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Class"> <EditItemTemplate> <asp:TextBox ID="txtClass" runat="server" Text='<%# Eval("Class") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblClass" runat="server" Text='<%# Eval("Class") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Edit/Delete"> <ItemTemplate> <asp:LinkButton runat="server" ID="linkedit" CommandName="Edit" CausesValidation="true" Text="Edit"></asp:LinkButton> &nbsp;&nbsp; <asp:LinkButton runat="server" ID="linkdelete" CommandName="Delete" CausesValidation="true" Text="Delete"></asp:LinkButton> </ItemTemplate> <EditItemTemplate> <asp:LinkButton runat="server" ID="linkupdate" CommandName="Update" CausesValidation="true" Text="Update"></asp:LinkButton> &nbsp;&nbsp; <asp:LinkButton runat="server" ID="linkcancel" CommandName="Cancel" CausesValidation="true" Text="Cancel"></asp:LinkButton> </EditItemTemplate> </asp:TemplateField> </Columns> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#999999" /> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> </asp:GridView>

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int Id = (int)GridView1.DataKeys[e.RowIndex].Value ; con.Open(); TextBox txtNewName = (TextBox )GridView1.Rows[e.RowIndex].FindControl("txtName"); TextBox txtNewAge = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAge"); TextBox txtNewClass = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtClass"); SqlCommand cmd = new SqlCommand ("Update Student set Name=@Name,Age=@Age,Class=@Class where ID=" + Id + "", con); string n = txtNewName.Text; cmd.Parameters.AddWithValue("@Name", txtNewName.Text); cmd.Parameters.AddWithValue("@Age", txtNewAge.Text); cmd.Parameters.AddWithValue("@Class", txtNewClass.Text); cmd.ExecuteNonQuery(); GridView1.EditIndex = -1; con.Close(); databind(); }

单击更新按钮后,它会显示以前的值,而不是更新值.我得到的是编辑前的值,而不是用户输入的值.

after clicking the update button it''s showing me the previous value and not updating the values.I am getting the value that was before editing not the value that was being put in by the user. what should i do to retrieve the new value ???

推荐答案

伙计们 要更新网格视图中的行,您需要将主ID置于视图状态,然后在该ID处调用该ID并调用其他参数. Hi dude To update row in grid view you need to take your main Id in view state then call that Id where you will call this Id and will call other parameters.

更多推荐

更新GridView时出现问题

本文发布于:2023-11-23 20:37:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622795.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:GridView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!