locked
Previous postback control fires again after new postback fires RRS feed

  • Question

  • User1310055179 posted

    Hi,

    I have a really weird problem that I can't even find the exact scenario that causes it.

    My application is a dynamic form created almost entirely from code behind. 

    In the web form, there is a password input that the user eventually needs to fill-in, however, sometimes the password seems to be wrong according to the code, even though it's the right one, and sometimes it's ok.

    When I started debugging I found out that randomly, after the first postback, another one is created, but with the value of the previous postback.

    For example, if I fill Textbox A with "BBB", and then I enter the password "CCC", I can see that the "CCC" value of the password is fired to the server and is being validated and updated correctly. After that I can see another postback for the password field, but with the "BBB" value. that is what prevents the password from being validated correctly.

    1. CreateChildControls()

    2.CreateDynamicForm()

    3.Page_Load()

    4. control_TextChanged() - Event Handler changes the password value to "CCC"

    5.CreateSynamicForm()

    6.CreateChildControls()

    7.Page_Load()

    8.control_TextChanged() - Event Handler changes the password value to "BBB"-  wrong value

    9.CreateDynamicForm() 

    Does anyone have any idea what can cause such a random problem? 

    uTextBox = new System.Web.UI.WebControls.TextBox();
                        uTextBox.ID = FldType + "_" + i;
                        uTextBox.AutoPostBack = true;
                        uTextBox.ToolTip = Tooltip;
                        uTextBox.Style.Add("left""0mm");
                        uTextBox.Style.Add("top""0mm");
                        uTextBox.Style.Add("position""relative");
                        uTextBox.TabIndex = short.Parse(i.ToString());
                        uTextBox.Attributes.Add("size", width.ToString());
                        uTextBox.Style.Add("text-align", cfg2Htm_Side(cfg[i, 10]));
                        uTextBox.Style.Add("font-size", font_size + "px");
                        uTextBox.Style.Add("font-family""monospace");
                        uTextBox.Style.Add("z-index", i.ToString());
                        uTextBox.MaxLength = width;
     
                        if (recStrReadonly[i] == true)
                        {
                            uTextBox.ReadOnly = true;
                        }
     
                        uPassTextBox = new System.Web.UI.WebControls.TextBox();
                        uPassTextBox.ID = FldType + "_" + i + "_Password";
                        uPassTextBox.AutoPostBack = true;
                        uPassTextBox.ToolTip = Tooltip;
                        uPassTextBox.TextMode = TextBoxMode.Password;
                        uPassTextBox.Attributes.Add("autocomplete""new-password");
                        uPassTextBox.Style.Add("left""0mm");
                        uPassTextBox.Style.Add("top""0mm");
                        uPassTextBox.Style.Add("position""relative");
                        uPassTextBox.Style.Add("z-index", i.ToString());
                        uPassTextBox.TabIndex = short.Parse(i.ToString());
                        uPassTextBox.Attributes.Add("size", width.ToString());
                        uPassTextBox.Style.Add("text-align", cfg2Htm_Side(cfg[i, 10]));
                        uPassTextBox.Style.Add("font-size", font_size + "px");
                        uPassTextBox.Style.Add("font-family""monospace");
                        uPassTextBox.MaxLength = width;
                        uPassTextBox.TextChanged += new EventHandler(control_TextChanged);
                        uPassTextBox.Attributes.Add("onchange""skm_LockScreen('skm_LockPane');");
    

    Inside control_TextChanged() this is the code that causes all the problems:

    System.Web.UI.WebControls.TextBox c = sender as System.Web.UI.WebControls.TextBox;
    						System.Diagnostics.Debug.WriteLine("c: " + c);
    						System.Diagnostics.Debug.WriteLine("c.Text " + c.Text);
    						val = c.Text;

    For some reason, c is the previous control that triggered a postback and not the password control.

     

    Wednesday, January 23, 2019 9:45 AM

Answers

  • User1310055179 posted

    Hi,

    I finally found out what was causing my form to act crazy.

    After I post back the signature value, I Hide the field that triggered the postback.

    uPassTextBox.Visible = false;
    

    When I removed that line it all seems to work just fine.

    Thanks all for trying to help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2019 10:12 AM

All replies

  • User-893317190 posted

    Hi qsoft_developer,

    I have a test for  dynamic form , but I couldn't reproduce your problem.In my case, the sender always contains current value which I have entered.

    Below is my code.

      protected void Page_Load(object sender, EventArgs e)
            {
                TextBox uPassTextBox = new TextBox();
                uPassTextBox = new System.Web.UI.WebControls.TextBox();
                uPassTextBox.ID = "_Password";
                uPassTextBox.AutoPostBack = true;
              
            ///    uPassTextBox.TextMode = TextBoxMode.Password;
                uPassTextBox.Attributes.Add("autocomplete", "new-password");
                uPassTextBox.Style.Add("left", "0mm");
                uPassTextBox.Style.Add("top", "0mm");
                uPassTextBox.Style.Add("position", "relative");
              
                
                uPassTextBox.Style.Add("font-family", "monospace");
             
                uPassTextBox.TextChanged += new EventHandler(TextBox1_TextChanged);
                this.Form.Controls.Add(uPassTextBox);
    
            }
    
            protected void TextBox1_TextChanged(object sender, EventArgs e)
            {
                Response.Write((sender as TextBox).Text);
            }

    And the result.

    According to your description, it  seems your  control_TextChanged are triggered two times.

    Please check all your code which binds this event to a control,maybe you bind this event to mutiple controls and they all trigger the event.

    If you still couldn't solve your problem, please post  enough code to let us reproduce your problem.

    In addition, I don't know why you need to dynamically add controls instead of add controls in the aspx,since adding control to aspx is a more simple and efficient way, it will help create the control automatically and save the state of the control.

    Best regards,

    Ackerly Xu

    Thursday, January 24, 2019 1:59 AM
  • User1310055179 posted

    Hi,

    Thanks for your reply.

    I finally managed to track a scenario.

    The additional postback only occurs if another field (TextBox with TextBoxMode.Multiline) actually contains a multiple line value (Even if I only type 'Enter' key inside the field).

    For example, if

    1. I add:

    "<g class="gr_ gr_531 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling" id="531" data-gr-id="531">sfdsdf</g>

    <g class="gr_ gr_559 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="559" data-gr-id="559">fffsg</g>"

    to a multiline textbox.

    2. Fill in some more fields.

    3. Add the password textbox

    4. Two postbacks are sent to the server. the first one is the password postback, and the last one is the multiline textbox (again).

    Here is the code I use to build the multiline textbox:

    				System.Web.UI.WebControls.TextBox HTextBox = (System.Web.UI.WebControls.TextBox)MainForm.FindControl("H_" + i);
    				if (HTextBox != null)
    				{//if the field's value was changed by another field (F)
    					MainForm.Controls.Remove(HTextBox);
    				}
    				
    				
    					HTextBox = new System.Web.UI.WebControls.TextBox();
    					HTextBox.ID = "H_" + i;
    					HTextBox.AutoPostBack = true;
    					HTextBox.ToolTip = Tooltip;
    					HTextBox.Style.Add("left", Col + "mm");
    					HTextBox.Style.Add("top", Row + "mm");
    					HTextBox.Style.Add("position""absolute");
    					HTextBox.Style.Add("z-index", i.ToString());
    					HTextBox.TabIndex = short.Parse(i.ToString());
    					HTextBox.Attributes.Add("width", widthH.ToString());
    					HTextBox.Style.Add("text-align", cfg2Htm_Side(cfg[i, 10]));
    					HTextBox.Style.Add("font-size", font_size + "px");
    					HTextBox.Style.Add("font-family""monospace");
    					HTextBox.Attributes.Add("onchange""skm_LockScreen('skm_LockPane');");
    					if (Convert.ToInt32(maxlength) < 256 && cfg[i, 7].Replace(" """) != "1" && width < 256)
    					{//if this is a memo text- don't define a max length
    						HTextBox.MaxLength = maxlength;
    					}
    					HTextBox.Rows = rows;
    					HTextBox.Columns = width;
     
    					if (cfg[i, 9].Length == 0 || cfg[i, 9] == "1")
    					{//only 1 text line
    						HTextBox.Attributes.Add("size", width.ToString());
    						HTextBox.TextMode = TextBoxMode.SingleLine;
    					}
    					else
    					{
    						HTextBox.TextMode = TextBoxMode.MultiLine;
    					}
     
    					HTextBox.TextChanged += new EventHandler(control_TextChanged);
    					HTextBox.Style[HtmlTextWriterStyle.Direction] = dir;
    					HTextBox.Text = recStr[i];
    					if (formStatus == 4 || recStrReadonly[i] == true)//if status = VIEW
    					{
    						HTextBox.ReadOnly = true;
    						HTextBox.BackColor = System.Drawing.ColorTranslator.FromHtml("#EBEBE4");
    					}
    					else
    					{
    						HTextBox.ReadOnly = false;
    					}
     
    					if (!recStrVisible[i])
    					{
    						HTextBox.Visible = false;
    					}
    					else
    					{
    						HTextBox.Visible = true;
    					}
    					if (recStr_F_B_Colors[i, 0] != "")
    					{
    						HTextBox.ForeColor = System.Drawing.ColorTranslator.FromHtml(recStr_F_B_Colors[i, 0]);
    					}
    					if (recStr_F_B_Colors[i, 1] != "")
    					{
    						HTextBox.BackColor = System.Drawing.ColorTranslator.FromHtml(recStr_F_B_Colors[i, 1]);
    					}
    					HTextBox.EnableViewState = false;
    					MainForm.Controls.Add(HTextBox);
     
    					if (i == focus)
    					{
    						HTextBox.Focus();
    					}
    				 
     

    In addition, I don't know why you need to dynamically add controls instead of <g class="gr_ gr_43 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="43" data-gr-id="43">add</g> controls in the <g class="gr_ gr_36 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="36" data-gr-id="36">aspx</g><g class="gr_ gr_45 gr-alert gr_gramm gr_inline_cards gr_run_anim Style replaceWithoutSep" id="45" data-gr-id="45">,since</g> adding <g class="gr_ gr_42 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="42" data-gr-id="42">control</g> to <g class="gr_ gr_37 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="37" data-gr-id="37">aspx</g> is a more simple and efficient way, it will help create the control automatically and save the state of the control.

    My application generates different web forms that were defined in another tool that outputs the definition to a text file.

    I read the text file and create the form dynamically.

        

    Thursday, January 24, 2019 10:11 AM
  • User-893317190 posted

    Hi qsoft_developer,

    It seems in your code 

                                        else
    					{
    						HTextBox.TextMode = TextBoxMode.MultiLine;
    					}
     
    					HTextBox.TextChanged += new EventHandler(control_TextChanged);
    

    You also bind control_TextChanged to the control HTextBox with mutiple line, which causes  your form to post back and trigger the event when  you leave the mutipleline textbox.

    By default, when you type 'Enter' key, the form will also submit and post back, so if you don't want to post back , please don't type Enter.

    Best regards,

    Ackerly Xu

    Friday, January 25, 2019 1:32 AM
  • User1310055179 posted

    Hi,

    I finally found out what was causing my form to act crazy.

    After I post back the signature value, I Hide the field that triggered the postback.

    uPassTextBox.Visible = false;
    

    When I removed that line it all seems to work just fine.

    Thanks all for trying to help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2019 10:12 AM