2013-04-30 238 views
1

我在這裏更新gridview的值,但該值沒有更新.. TextBox txtID,TextBox txtName,TextBox txtAge只保留舊的價值和價值沒有得到任何人updated..Can電話我喜歡我做錯了什麼在這裏Gridview編輯/更新不起作用?

這裏是我的代碼

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); 
     } 

任何建議??

EDIT(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) 
     { 

     } 
+0

也把你的Page_Load事件代碼well..thanks – Rahul 2013-04-30 06:57:45

回答

0

Page Life Cycle ,你會得到,如果你想在綁定代碼後,你應該這樣做在初始化事件,其他明智的沒有事件將觸發。

+0

看看我的編輯.. – Rooney 2013-04-30 07:02:49

0

這似乎是Page.IsPostback problem.Need在你的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事件中,就像我之前提到的一樣。

我希望它有幫助。

+0

看看我的編輯.. – Rooney 2013-04-30 07:02:18

+0

你的Session [「dataSetInSession」]爲具有舊值和UR分配相同的價值,你的gvTemp (網格視圖)gvTemp_RowUpdating事件。這是我認爲的問題。當您調用gvTemp_RowUpdating事件時,請檢查dataSetInSession是否爲null。 – Rahul 2013-04-30 07:06:50