为什么我得到GridViewUpdateEventArgs#NewValues​​和GridViewUpdateEventArgs#OldValues​​空的? [英] Why I'm getting GridViewUpdateEventArgs#NewValues and GridViewUpdateEventArgs#OldValues empty?

查看:360
本文介绍了为什么我得到GridViewUpdateEventArgs#NewValues​​和GridViewUpdateEventArgs#OldValues​​空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我GridView控件绑定像 -

I'm binding GridView like -

namespace grid_edit
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data source=HP_OWNER\\SQLEXPRESS;Database=demo;Integrated security=true";
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from demo", con);
            cmd.ExecuteNonQuery();
            SqlDataAdapter mySqlAdapter = new SqlDataAdapter(cmd);
            DataSet myDataSet = new DataSet();
            mySqlAdapter.Fill(myDataSet);
            DataTable myDataTable = myDataSet.Tables[0];
            GridView1.DataSource = myDataTable;
            GridView1.DataBind();
            Session["mytable"] = myDataTable;


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

        }       

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            String updatedItem = e.NewValues[0].ToString();
            String anotherUpdatedItem = e.NewValues[1].ToString();

        }

    }
}

和马克有像下面 -

and having Mark up like following -

<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True" 
            BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" 
            CellPadding="4" CellSpacing="2" ForeColor="Black"            
        style="top: 63px; left: 309px; position: absolute; height: 183px; width: 312px; margin-top: 25px;" 
        OnRowEditing="GridView1_RowEditing" 
        OnRowCancelingEdit="GridView1_RowCancelingEdit"        
        OnRowUpdating="GridView1_RowUpdating" >
            <RowStyle BackColor="White" />
            <FooterStyle BackColor="#CCCCCC" />
            <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />

        </asp:GridView>

现在我想从网格视图但在更新事件更新的行 GridViewUpdateEventArgs 我越来越NewValues​​和OldValues​​空。

我怎样才能获得更新的值在数据库存储?难道我做错了吗?

Now I would like to update the rows from the grid view but in the update event GridViewUpdateEventArgs I'm getting NewValues and OldValues empty.
How can I get the the updated values to store in the DB ? Am I doing something wrong here ?

推荐答案

尝试改变code如下:

Try to change the code as follows:

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data source=(local);Database=northwind;Integrated security=true";
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Categories", con);
        cmd.ExecuteNonQuery();
        SqlDataAdapter mySqlAdapter = new SqlDataAdapter(cmd);
        DataSet myDataSet = new DataSet();
        mySqlAdapter.Fill(myDataSet);
        DataTable myDataTable = myDataSet.Tables[0];
        GridView1.DataSource = myDataTable;
    if(!IsPostBack)  /// <<<<<<<<<<<<<<<<<<<<<<
        GridView1.DataBind();
        Session["mytable"] = myDataTable;

    }

在这里工作得很好.NET Framework 4中。

It works fine here for .NET Framework 4.

由于您使用的.NET Framework 3.0,这code不起作用。我已经看过GridView的HandleUpdate方法,并发现,当在GridView势必通过DataSourceID属性一个DataSourceControl这些集合(e.NewValues​​和e.OldValues​​)只填充...一个可能的解决方案是使用ExtractValuesFromCell方法:

Since you are using .NET Framework 3.0, this code does not work. I have looked at the GridView's HandleUpdate method and found out that these collections (e.NewValues and e.OldValues) are only populated when the Gridview is bound to a DataSourceControl through DataSourceID property... A possible solution is to use the ExtractValuesFromCell method:

    cell = GridView1.Rows[e.RowIndex].Cells[1] as DataControlFieldCell;
    GridView1.Columns[1].ExtractValuesFromCell(
        e.NewValues,
        cell,
        DataControlRowState.Edit,
        true);

请注意:Page_Load方法应该有code作为我的建议,即应的DataBind调用一次

NOTE: the Page_Load method should have the code as I suggested, i.e. the DataBind should called only once.

这篇关于为什么我得到GridViewUpdateEventArgs#NewValues​​和GridViewUpdateEventArgs#OldValues​​空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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