Gridview rowupdating命令无法正常工作 [英] Gridview rowupdating command is not working properly

查看:67
本文介绍了Gridview rowupdating命令无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我在更新gridview中的行时遇到问题.当我试图从gridview更新一行,然后在rowupdating命令中,当我试图从"gridvew文本框访问新值时,它从文本框中访问行的旧值,而不是新的行" 再次更新旧值,而不是新值.我的代码是..



hello everyone,
I am facing a problem in updating a row in gridview. when i am trying to update a row from gridview then in rowupdating command when i m trying to access new value from the "gridvew textboxes it access the old value of row from text box instead of new one" then it updating oldone value again instead of new one. my code is..



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

       onrowcommand="GridView1_RowCommand"

       onrowdatabound="GridView1_RowDataBound" CellPadding="4" ForeColor="#333333"

       GridLines="None" onrowediting="GridView1_RowEditing"

       onrowupdating="GridView1_RowUpdating"

       onrowcancelingedit="GridView1_RowCancelingEdit"

       onrowupdated="GridView1_RowUpdated">
       <AlternatingRowStyle BackColor="White" />
       <Columns>
           <asp:TemplateField HeaderText="Employee No.">
               <ItemTemplate>
                   <asp:Label ID="Label1" runat="server" Text='<%# Bind("emp_no") %>'></asp:Label>
               </ItemTemplate>
               <EditItemTemplate>
                   <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("emp_no") %>'></asp:TextBox>
               </EditItemTemplate>
               <FooterTemplate>
                   <asp:TextBox ID="txtemp_no" runat="server" Text=''></asp:TextBox>
               </FooterTemplate>
               <HeaderTemplate>
                   Employee No
               </HeaderTemplate>

           </asp:TemplateField>


           <asp:TemplateField HeaderText="Employee Name" SortExpression="emp_name">
               <EditItemTemplate>
                   <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("emp_name") %>'></asp:TextBox>
               </EditItemTemplate>
               <FooterTemplate>
                   <asp:TextBox ID="TextBox3" runat="server" Text=''></asp:TextBox>
               </FooterTemplate>
               <HeaderTemplate>
                   Employee Name
               </HeaderTemplate>
               <ItemTemplate>
                   <asp:Label ID="Label2" runat="server" Text='<%# Bind("emp_name") %>'></asp:Label>
               </ItemTemplate>
           </asp:TemplateField>



           <asp:TemplateField HeaderText="Salary" SortExpression="salary">
               <EditItemTemplate>
                   <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("salary") %>'></asp:TextBox>
               </EditItemTemplate>
               <FooterTemplate>
                   <asp:TextBox ID="TextBox5" runat="server" Text=''></asp:TextBox>
               </FooterTemplate>
               <HeaderTemplate>
                   Salary
               </HeaderTemplate>
               <ItemTemplate>
                   <asp:Label ID="Label3" runat="server" Text='<%# Bind("salary") %>'></asp:Label>
               </ItemTemplate>
           </asp:TemplateField>



           <asp:TemplateField HeaderText="city">
               <EditItemTemplate>
                   <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
               </EditItemTemplate>
               <FooterTemplate>
                   <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
               </FooterTemplate>
               <HeaderTemplate>
                   City
               </HeaderTemplate>
               <ItemTemplate>
                   <asp:Label ID="Label4" runat="server" Text='<%# Bind("city") %>'></asp:Label>
               </ItemTemplate>
           </asp:TemplateField>
           <asp:CommandField ShowEditButton="True" />


           <asp:CommandField ShowSelectButton="True" />


       </Columns>
       <EditRowStyle BackColor="#2461BF" />
       <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
       <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
       <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
       <RowStyle BackColor="#EFF3FB" />
       <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
       <SortedAscendingCellStyle BackColor="#F5F7FB" />
       <SortedAscendingHeaderStyle BackColor="#6D95E1" />
       <SortedDescendingCellStyle BackColor="#E9EBEF" />
       <SortedDescendingHeaderStyle BackColor="#4870BE" />
   </asp:GridView>





 protected void Page_Load(object sender, EventArgs e)
        {
            con = new SqlConnection("Data Source=KRN-PC;Initial Catalog=my_first_database;Integrated Security=True");
            Grid();
        }
        private void Grid()
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from employee_table", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "tab1");
            //ds = (DataSet)Session["s1"];
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
          
        }

      

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //Get the values stored in the text boxes
           string emp_name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
            int salary = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4")).Text);
            string city = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6")).Text;
            int emp_no = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text);

            
            //Prepare the Update Command of the DataSource control
            string strSQL = "";
            strSQL = "UPDATE employee_table set emp_name = '" + emp_name + "'" +
                     ",salary = '" + salary + "'" +
                     ",city = '" + city + "'" + "WHERE emp_no='" + emp_no + "'";

            da = new SqlDataAdapter(strSQL, con);
            ds = new DataSet();
            da.Fill(ds, "tab1");
            GridView1.EditIndex = -1;
            Grid();
            ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Customer updated successfully');
           }

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
       {
           GridView1.EditIndex = e.NewEditIndex;
           Grid();
       }

       protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
       {

       }

       protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
       {
           GridView1.EditIndex = -1;
           Grid();
       }

推荐答案

您的网格函数应该不在回发块中


your grid function should be in not postback block


protected void Page_Load(object sender, EventArgs e)
       {
            if (!IsPostBack)
            {
           con = new SqlConnection(Data Source=KRN-PC;Initial Catalog=my_first_database;Integrated Security=True);
           Grid();
            }
        }


这篇关于Gridview rowupdating命令无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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