Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,

I am currently trying to use a code-behind created TextBox as the trigger for a TextBox_Changed event. I am able to get the TextBox created and even able to get EventHandler registered and assigned to the TextBox. However when I get to Debugging, I am receiving a browser error message in IE (I must develop for this browser - company guideline!). The error is 'TextBox_TextChanged' is undefined.

Here is my Code for creating the TextBox (actually a series of boxes, but it is the same for each one) -

//  If session variable is visible in the grid, add its text box and description one column over

if (sessVars[i].Keyvis == true)
{
    TextBox txtBxNm = new TextBox();
    txtBxNm.Width = 48;
    txtBxNm.ID = "txtBoxNomPt" + rw + cl;
    txtBxNm.AutoPostBack = true;
    txtBxNm.Attributes.Add("OnChange", "TextBox_TextChanged");
    txtBxNm.TextChanged += new EventHandler(TextBox_TextChanged);
    this.NomFlds.Rows[rw].Cells[cl].Controls.Add(txtBxNm);
    if (sessVars[i].Value != "" && sessVars[i] != null)
    {
        txtBxNm.Text = sessVars[i].Value.ToString();
    }

    this.NomFlds.Rows[rw].Cells[cl + 1].Text = sessVars[i].PosDesc.ToString();
}


The above Code appears inside my page_load / !=IsPostBack load of the page. Later on in the class code-behind I have the following method code for the event -

protected void TextBox_TextChanged(object sender, EventArgs e)
       {
           sql_val nom_p_des = new sql_val();
           Control control = (Control)sender;
           TextBox thisctrl = new TextBox();
           control = (Control)thisctrl;
           String ctrlName = control.ID;
           int nmlen = ctrlName.Length;
           int rmdr = nmlen - 11;
           int posid = int.Parse(ctrlName.Substring(12, rmdr).ToString());
           nom_p_des.Num_whr_flds = 0;

           switch (posid)
           {
               case 00:
                   nom_p_des.Frm_table = "Position1";
                   nom_p_des.Keycall = true;
                   nom_p_des.Dtypefld = "str";
                   nom_p_des.Keypos = 1;

                   break;
               case 01:
                   nom_p_des.Frm_table = "Position2";
                   nom_p_des.Keycall = true;
                   nom_p_des.Dtypefld = "str";
                   nom_p_des.Keypos = 2;

                   break;


….. It continues (but I don't make it this far!)

What I have tried:

I have tried changing the event (first I had the problem it wasn't even firing, then I changed to TextBoxChanged and it started firing - good). Now I am beginning to think there is some sort of rendering issue. When I look at Browser Debugger versions of other standard TextBox_Changed Events in other pages, they are render liked this inside the HTML -

onchange="javascript:setTimeout('__doPostBack(\'ctl00$TurbineWheel$TWExdDiam\',\'\')', 0)"


When I look at the Browser Debugger version of the Events for the code-behind generated Textboxes, they look like this after rendering in HTML -

onchange="TextBox_TextChanged;setTimeout('__doPostBack(\'


So it appears as if the conversion to JavaScript is missing. Not sure why. I would appreciate any / all help. Thanks.

---Ray
Posted
Updated 13-Sep-19 4:33am
v2
Comments
F-ES Sitecore 13-Sep-19 9:56am    
Are you creating your textboxes and attaching the events on every postback, not just the initial page load?
Ray Fischer 13-Sep-19 10:10am    
Hi F-ES,

Yes, I do have a section in the page load for that as well. But it doesn't get that far. As soon as I make a Change Event fire, it gives me the error. Here is my (else) for the !=IsPostback on Page_Load -
if (sessVars[i].Keyvis == true)
{
TextBox txtBxNm = new TextBox();
txtBxNm.Width = 48;
txtBxNm.ID = "txtBoxNomPt" + rw + cl;
txtBxNm.AutoPostBack = true;
txtBxNm.Attributes.Add("runat", "server");
txtBxNm.Attributes.Add("OnChange", "TextBox_TextChanged");
txtBxNm.TextChanged += new EventHandler(TextBox_TextChanged);
this.NomFlds.Rows[rw].Cells[cl].Controls.Add(txtBxNm);
if (sessVars[i].Value != "" && sessVars[i] != null)
{
txtBxNm.Text = sessVars[i].Value.ToString();
}

                    this.NomFlds.Rows[rw].Cells[cl + 1].Text = sessVars[i].PosDesc.ToString();
                }


So it is basically the same thing. NomFlds is simply a GridView which contains the text box controls.
F-ES Sitecore 13-Sep-19 11:06am    
What are you doing to "make it fire"? If you're doing something programatically then don't. The proper sequence is that you attach the changed event on page load, after page load has finished running .net will determine what events need to fire, and when it detects your textbox has changed (via a mix of the form and the ViewState) it will call the changed event, and as you attached it on page load it should all just work.
Ray Fischer 16-Sep-19 7:30am    
Hi F_ES,

Yes that was my understanding too. The problem is it is still not firing. The Event I am linking the EventHandler to is OnChange. I am not trying to fire it programmatically. It does detect that my text_box has been chagnged and performs a PostBack. But ist simply doesn't reach out to the EventHandler. ---Ray
Ray Fischer 16-Sep-19 8:29am    
Sorry F_ES, I did get it to fire after all. Right after I responded to your comment, I saw Richard's solution and it worked. I am running on now. Worked great. Thanks for the support and assistance.

1 solution

Quote:
C#
txtBxNm.Attributes.Add("OnChange", "TextBox_TextChanged");
You're setting the client-side "changed" handler to a Javascript function called TextBox_TextChanged. That Javascript function does not exist.

You've already attached the server-side event handler on the next line. You just need to remove the Attributes.Add line.
 
Share this answer
 
Comments
Ray Fischer 16-Sep-19 8:31am    
Thanks Richard, that did the trick. In some examples online which I saw about programmatically created controls, they had both the EventHandler and the Attribute creation listed as necessary for the created control object (?). Great support, man.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900