更新GridView时出现问题 [英] Problem in updating gridview

查看:67
本文介绍了更新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时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆