0

I'm dynamically generating a textbox and a dropdownlist in my ASP.NET web app table row, I want to change value of textbox based on dropdownlist selectedindex using javascript, but I don't know how to pass these dynamically created controls to my Javascript function.

                    TextBox t = new TextBox();
                tc.Controls.Add(t);
                tr.Cells.Add(tc);

                tc = new TableCell();
                DropDownList ddl = new DropDownList();
                ddl.Attributes.Add("onChange", "return OnFoodChange(this," + t + ");");
                tc.Controls.Add(ddl);

I pass 'this' instead of my combobox, and it works fine, but textbox is not detected in my following javascript function:

               function OnFoodChange(myCmb,myTxt) {
try{
               var q = document.getElementById('<%= HFFoodPrice.ClientID %>').value.toString();
               var q2 = q.split(';');
               alert(myCmb.selectedIndex.toString());
               alert(document.getElementById(myTxt.value));
               for (var j = 0; j < q2.length; j++) {
                   if (q2[j] != '') {
                       var q3 = q2[j].split(',');
                   {

                   }
               }
           }
           }
           catch(err)
           {
           alert(err.message);
           }
       }

what is the correct way of passing dynamically created controls to a javascript function? should I set controls ID in my codebehind?

1 Answer 1

1

In C#:

ddl.Attributes.Add("onChange", "return OnFoodChange(this," + t.ClientID + ");");

in Javascript, try this:

alert(document.getElementById(myTxt).value);
Sign up to request clarification or add additional context in comments.

1 Comment

it seems that my javascript function is not called at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.