1

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

        }
4

2 回答 2

0

阅读页面生命周期 你会得到,如果你想在代码隐藏中绑定,你应该在 Init Event 中进行,否则不会触发任何事件。

于 2013-04-30T06:53:30.520 回答
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 事件中,就像我提到的那样。

我希望它有所帮助。

于 2013-04-30T06:54:10.660 回答