How to maitain value on postback

Last post 04-20-2009 10:48 AM by Sonu. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 04-15-2009 1:29 AM

    • shahid13384
    • Top 50 Contributor
    • Joined on 06-09-2008
    • United States of Mehsana
    • Wannabe Slacker
    • Points 563

    How to maitain value on postback

    Hi all,  I do have one function that generate runtime server cotrols. i do call this function on (!page.ispostback). as follows  

    if (!Page.IsPostBack && NullifySession())
            {

                Session["RegisterEmptyOnBlur"] = "true";
                PageOperation.Mode = PageOperationMode.List;
                GenerateForm(); // This is my function
            }

    but when click on save button the page got black.and there is no contro. If i write GenerateForm() function out side  IF(!page.ispostback). still the same problem any suggestion to overcome this problem

     

     

    Thanks & Regards


    The trouble with the world is that the stupid are sure and the intelligent are full of doubt.

    Shahid Hussain Shaikh
  •  Advertisement

    Featured Advertisement

     
  • 04-15-2009 4:14 AM In reply to

    • Quango
    • Not Ranked
    • Joined on 05-27-2008
    • Wannabe Slacker
    • Points 8

    Re: How to maitain value on postback

    Any code that generates controls dynamically must be run for EVERY page generation, even on postback. So yes you should have GenerateForm() outside of the !IsPostBack IF statement.

    Why it isn't working outside the IF is probably due to an error somewhere in the GenerateForm code (it must work on the first page run?)

  • 04-15-2009 7:29 AM In reply to

    • Sonu
    • Top 10 Contributor
    • Joined on 05-22-2006
    • Montreal / Canada
    • Slacker
    • Points 12,183
    • MVP

    Re: How to maitain value on postback

    Controls must be re-created on every round-trip. Take a look at this article: http://www.4guysfromrolla.com/articles/081402-1.aspx

    [MVP since 2005] [MCAD]
    Webmaster of DotNetSlackers
    Question or Suggestion?
    Feel free to ask my any .NET question
    Our Posting FAQ
  • 04-16-2009 9:04 AM In reply to

    • shahid13384
    • Top 50 Contributor
    • Joined on 06-09-2008
    • United States of Mehsana
    • Wannabe Slacker
    • Points 563

    Re: How to maitain value on postback

    i took another approch. i am using Javascript hasttable you can get from  Here.Onblur event of each input fied m storing the value of control in hashtable and contorl name as key to the value.then storing this as string in Hidden field and then on click of save button use  the value stored in hidden field

    Thanks & Regards


    The trouble with the world is that the stupid are sure and the intelligent are full of doubt.

    Shahid Hussain Shaikh
  • 04-16-2009 9:11 AM In reply to

    • Sonu
    • Top 10 Contributor
    • Joined on 05-22-2006
    • Montreal / Canada
    • Slacker
    • Points 12,183
    • MVP

    Re: How to maitain value on postback

    shahid13384:
    i am using Javascript hasttable

    Shahid, that is a nice library, however for this approach I would rather try to implement dynamic controls properly.

    [MVP since 2005] [MCAD]
    Webmaster of DotNetSlackers
    Question or Suggestion?
    Feel free to ask my any .NET question
    Our Posting FAQ
  • 04-17-2009 1:48 AM In reply to

    • shahid13384
    • Top 50 Contributor
    • Joined on 06-09-2008
    • United States of Mehsana
    • Wannabe Slacker
    • Points 563

    Re: How to maitain value on postback

    Hi sonu, I didn't get your meanig by saying

    sonu:
    implement dynamic controls
    . my controls works perfectly. The only problem is control's value got cleared on post back. i'll  will be very thankful,  if got some good suggestion from experts like you and other contributers on this forum.

    Thanks & Regards


    The trouble with the world is that the stupid are sure and the intelligent are full of doubt.

    Shahid Hussain Shaikh
  • 04-17-2009 7:55 AM In reply to

    • Sonu
    • Top 10 Contributor
    • Joined on 05-22-2006
    • Montreal / Canada
    • Slacker
    • Points 12,183
    • MVP

    Re: How to maitain value on postback

    You are creating controls dynamically on the pageLoad via the GenerateForm function, correct? To make sure that dynamic controls maintain their state and value you need to create them early in the page life cycle. Again, I strongly recommend you to read this quick article: http://www.4guysfromrolla.com/articles/081402-1.aspx

    Can you also show me your GenerateForm() method?

    [MVP since 2005] [MCAD]
    Webmaster of DotNetSlackers
    Question or Suggestion?
    Feel free to ask my any .NET question
    Our Posting FAQ
  • 04-20-2009 12:57 AM In reply to

    • shahid13384
    • Top 50 Contributor
    • Joined on 06-09-2008
    • United States of Mehsana
    • Wannabe Slacker
    • Points 563

    Re: How to maitain value on postback

    Ya sure sonu, Here it is:

     #region Generating the controls runtime
        protected void GenerateForm()
        {
            try
            {
                if (Session["LoginUser"] != null)
                {
                    LoginUser loginuser = (LoginUser)Session["LoginUser"];
                    string strCriteriaId = objIIP.GetCriteriaSequnce(ConfigurationSettings.AppSettings["DBProvider"], loginuser.Degree.YearCode.ToString(), loginuser.Degree.SessionNo.ToString(), loginuser.Degree.SysID.ToString());
                    DataSet dsCVData = objIIP.GetCVData(ConfigurationSettings.AppSettings["DBProvider"].ToString());
                   if (strCriteriaId != null && strCriteriaId.Length > 0)
                    {
                        char[ sep = new char[ { ',' };
                        string[ strSplitArr = strCriteriaId.Split(sep);
                        Panel2.Controls.Add(new LiteralControl("<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">"));
                        for (int i = 0; i < strSplitArr.Length; i++)
                        {
                            DataSet dsCriteriaValue = objIIP.GetCriteriaData(ConfigurationSettings.AppSettings["DBProvider"], strSplitArrIdea);
                            if (dsCriteriaValue != null)
                            {
                                /// Creating Lables
                                Label lblNew = new Label();

                                lblNew.ID = "lbl" + dsCriteriaValue.Tables[0].Rows[0]["Topic"].ToString();
                                lblNew.Text = dsCriteriaValue.Tables[0].Rows[0]["Topic"].ToString() + " :";
                                lblNew.CssClass = "content1";

                                //  Panel2.Controls.Add(new LiteralControl("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">"));

                                Panel2.Controls.Add(new LiteralControl("<tr>"));
                                Panel2.Controls.Add(new LiteralControl("<td width='20%'>"));
                                Panel2.Controls.Add(lblNew);
                                Panel2.Controls.Add(new LiteralControl("</td>"));
                                if (dsCriteriaValue.Tables[0].Rows[0]["ControlType"].ToString().ToLower().Substring(0, 1) == "m")
                                {

                                    /// Generating Multiline textbox
                                    ///
                                    TextBox txtNew = new TextBox();
                                    txtNew.ID = "txt" + dsCriteriaValue.Tables[0].Rows[0]["Topic"].ToString() + "~" + dsCriteriaValue.Tables[0].Rows[0]["SysID"].ToString();
                                    txtNew.TextMode = TextBoxMode.MultiLine;

                                   
                                   


                                    Panel2.Controls.Add(new LiteralControl("<td width='50%'>"));

                                   

                                    Panel2.Controls.Add(txtNew);
                                    txtNew.Attributes.Add("onblur", "BLOCKED SCRIPTreturn SetValue('" + txtNew.ClientID + "')"); // on onblur storing value in Jhastable.
                                    Panel2.Controls.Add(new LiteralControl("</td>"));
                                }
                                if (dsCriteriaValue.Tables[0].Rows[0]["ControlType"].ToString().ToLower().Substring(0, 1) == "s")
                                {
                                    TextBox txtNew = new TextBox();
                                    txtNew.ID = "txt" + dsCriteriaValue.Tables[0].Rows[0]["Topic"].ToString() + "~" + dsCriteriaValue.Tables[0].Rows[0]["SysID"].ToString();
                                    txtNew.TextMode = TextBoxMode.SingleLine;
                                    Panel2.Controls.Add(new LiteralControl("<td width='50%'>"));

                                    Panel2.Controls.Add(txtNew);
                                    txtNew.Attributes.Add("onblur", "BLOCKED SCRIPTreturn SetValue('" + txtNew.ClientID + "')");
                                    Panel2.Controls.Add(new LiteralControl("</td>"));
                                }
                                if (dsCriteriaValue.Tables[0].Rows[0]["ControlType"].ToString().ToLower().Substring(0, 1) == "d")
                                {
                                    DropDownList ddlNew = new DropDownList();
                                    ddlNew.ID = "ddl" + dsCriteriaValue.Tables[0].Rows[0]["Topic"].ToString() + "~" + dsCriteriaValue.Tables[0].Rows[0]["SysID"].ToString();
                                    ddlNew.Width = 100;
                                    ddlNew.Items.Add("Select..");
                                    string strDfault = dsCriteriaValue.Tables[0].Rows[0]["Defaultvalues"].ToString();

                                    char[ sepddl = new char[ { ',' };
                                    string[ strSplitArrddl = strDfault.Split(sepddl);

                                    for (int j = 0; j < strSplitArrddl.Length; j++)
                                    {
                                        ddlNew.Items.Add(strSplitArrddl[j]);
                                    }
                                    Panel2.Controls.Add(new LiteralControl("<td width='50%'>"));
                                    Panel2.Controls.Add(ddlNew);
                                    Panel2.Controls.Add(new LiteralControl("</td>"));
                                }
                                Panel2.Controls.Add(new LiteralControl("</tr>"));
                            }// if statstmet ends here.
                        } // for loop ends here.
                        Panel2.Controls.Add(new LiteralControl("</table >"));
                    }// if ends for criteria exist
                    else
                    {
                    }
                    //}// if end for checking student already enterd data
                    //else

                    //{

                    //    Panel2.Controls.Add(new LiteralControl(" You have already fill up the CV..!"));
                    //}
                }




            }
            catch (Exception ex)
            {
                Panel1.Visible = false;
                Label lb = new Label();
                lb.ID = "lblMsg";
                lb.Text = "No Criteria available for you..!";
                Panel2.Controls.Add(lb);
                EGovernance_DAL.Common.WriteErrorLOG(ConfigurationSettings.AppSettings["ErrorLogLocation"],
                        "IIP",
                        "ResumeDetailEntry",
                        "GenerateForm", ex);
            }





        }
        #endregion

    Thanks & Regards


    The trouble with the world is that the stupid are sure and the intelligent are full of doubt.

    Shahid Hussain Shaikh
  • 04-20-2009 10:48 AM In reply to

    • Sonu
    • Top 10 Contributor
    • Joined on 05-22-2006
    • Montreal / Canada
    • Slacker
    • Points 12,183
    • MVP

    Re: How to maitain value on postback

    Since you creating dynamic controls, I suggest you again that you take a look at this article:

    http://www.4guysfromrolla.com/articles/081402-1.aspx

    Dynamic controls need to be re-created on each postback and for them to work properly, you need to do that in correct event, which is usually page_init. The above article will show you the rest. 

    Hope this helps.

    [MVP since 2005] [MCAD]
    Webmaster of DotNetSlackers
    Question or Suggestion?
    Feel free to ask my any .NET question
    Our Posting FAQ
    Filed under:
Page 1 of 1 (9 items)