Gridview编辑/更新无效?

时间:2013-04-30 06:49:27

标签: c# asp.net gridview

这里我正在更新gridview值,但值没有更新.. TextBox txtID,TextBox txtName,TextBox txtAge仅保留旧值,值不会更新..任何人都可以告诉我,我在这里做错了什么

这是我的代码

protected void gvTemp_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            CreateDataSkeletton(Convert.ToInt16(Session["intTabIndex"]));
            GridViewRow row = (GridViewRow)gvTemp.Rows[e.RowIndex];
            int autoid = Int32.Parse(gvTemp.DataKeys[e.RowIndex].Value.ToString());

            int id = Convert.ToInt32(gvTemp.DataKeys[e.RowIndex].Values[0].ToString());
            activeTabIndex = Convert.ToInt16(Session["activeTabIndex"]);


            TextBox txtID = ((TextBox)gvTemp.Rows[e.RowIndex].FindControl("txtId"));
            TextBox txtName = (TextBox)gvTemp.Rows[e.RowIndex].FindControl("txtName");
            TextBox txtAge = (TextBox)gvTemp.Rows[e.RowIndex].FindControl("txtAge");

            dataSetInSession.Tables["Days" + activeTabIndex.ToString()].Rows[e.RowIndex]["ID"] = txtID.Text;
            dataSetInSession.Tables["Days" + activeTabIndex.ToString()].Rows[e.RowIndex]["Name"] = txtName.Text;
            dataSetInSession.Tables["Days" + activeTabIndex.ToString()].Rows[e.RowIndex]["Age"] = txtAge.Text;

            gvTemp.DataSource = dataSetInSession.Tables["Days" + activeTabIndex.ToString()];
            gvTemp.DataBind();
            gvTemp.EditIndex = -1;
        }

private void CreateDataSkeletton(int intTabIndex)
        {
            dataSetInSession = new DataSet();
            Session["intTabIndex"] = intTabIndex;

            if (Session["dataSetInSession"] != null)
            {
                dataSetInSession = (DataSet)Session["dataSetInSession"];
            }
            if (dataSetInSession.Tables["Days" + intTabIndex].Rows.Count > 0)
            {
                gvTemp.DataSource = dataSetInSession.Tables["Days" + intTabIndex];
                gvTemp.DataBind();
            }
            else
            {
                gvTemp.DataSource = dataSetInSession.Tables["Days"];
                gvTemp.DataBind();
            }
            temp.Controls.Add(gvTemp);
        }

有什么建议吗?

修改(1):

protected void Page_Init(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AddDataTable();
            }
            dataSetInSession = new DataSet();

            if (Session["dataSetInSession"] != null)
            {
                dataSetInSession = (DataSet)Session["dataSetInSession"];
            }

            if (Session["dynamicTabIDs"] != null)
            {
                //if dynamicTabIDs are in session, recreating the Tabs
                //that are associated with the Tab IDs
                //and adding them to the TabContainer that will contain
                //all of the dynamic tabs.

                //retrieving the tab IDs from session:
                dynamicTabIDs = (List<string>)Session["dynamicTabIDs"];
                if (TabContainerContent.ActiveTabIndex == -1)
                {
                    TabContainerContent.ActiveTabIndex = Convert.ToInt16(Session["intTabIndex"]);

                }
                CreateDataSkeletton(Convert.ToInt16(Session["intTabIndex"]));
                //looping through each TabID in session 
                //and recreating the TabPanel that is associated with that tabID
                foreach (string tabID in dynamicTabIDs)
                {


                    //creating a new TabPanel that is associated with the TabID
                    AjaxControlToolkit.TabPanel tab = new AjaxControlToolkit.TabPanel();
                    //TabContainerContent.ActiveTabIndex = tab;
                    //Setting the ID property of the TabPanel
                    tab.ID = tabID;
                    //setting the TabPanel's HeaderText
                    tab.HeaderText = "Days " + (TabContainerContent.Tabs.Count + 1).ToString();

                    //creating a Label to add to the TabPanel...at this point you'll have to
                    //create whatever controls are required for the tab...
                    Label tabContent = new Label();
                    //Giving the Label an ID
                    tabContent.ID = "lbl_tab_" + TabContainerContent.Tabs.Count.ToString();
                    //Setting the Label's text
                    tabContent.Text = "Tab " + (TabContainerContent.Tabs.Count + 1).ToString();

                    //Adding the Label to the TabPanel
                    tab.Controls.Add(tabContent);

                    //Adding the TabPanel to the TabContainer that contains the dynamic tabs
                    TabContainerContent.Tabs.Add(tab);
                }
            }
            else
            { //Creating a new list of dynamicTabIDs because one doesn't exist yet in session.
                dynamicTabIDs = new List<string>();
            }
        }

protected void Page_Load(object sender, EventArgs e)
        {

        }

2 个答案:

答案 0 :(得分:0)

阅读Page Life Cycle 你会得到的,如果你想在代码隐藏中绑定,你应该在Init Event中执行它,否则就没有事件会被触发。

答案 1 :(得分:0)

似乎 Page.IsPostback 问题。需要在page_load中添加 Page.IsPostback 属性,如

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
             // Put your code inside that. 
            }
        }

实际上你的控件正在获得新值,但是当你点击进行更新时,它会调用page_load中的旧值。所以尝试将Page.IsPostback放入你的page_load事件中,就像我提到的那样。

我希望它有所帮助。