Gridview编辑/更新不工作

本文关键字:工作 更新 编辑 Gridview | 更新日期: 2023-09-27 18:11:32

这里我正在更新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)
        {
        }

Gridview编辑/更新不工作

读取页面生命周期你会得到,如果你想在代码后绑定,你应该在Init Event中做,否则没有事件会触发

似乎是Page。IsPostback 问题。需要添加页面。在page_load中设置IsPostback属性,例如

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

实际上你的控制是得到新的值,但当你点击更新,然后它调用旧值从page_load。所以试着把佩奇。IsPostback到你的page_load事件,就像我提到的。