【问题标题】:Gridview Edit/Update is not working?Gridview 编辑/更新不起作用?
【发布时间】:2013-04-30 06:49:26
【问题描述】:

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

        }

【问题讨论】:

  • 也把你的 page_load 事件代码也放上......谢谢

标签: c# asp.net gridview


【解决方案1】:

阅读Page Life Cycle 你会发现,如果你想在代码隐藏中绑定,你应该在 Init Event 中进行,否则不会触发任何事件。

【讨论】:

    【解决方案2】:

    好像是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 事件中,就像我提到的那样。

    希望对你有帮助。

    【讨论】:

    • 您的 Session["dataSetInSession"] 具有旧值,并且您在 gvTemp_RowUpdating 事件中为您的 gvTemp(网格视图)分配了相同的值。这是我认为的问题。在调用 gvTemp_RowUpdating 事件时检查 dataSetInSession 是否为空。
    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 2011-05-01
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多