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?

Ali_dotNet
  • 3,219
  • 10
  • 64
  • 115

1 Answers1

1

In C#:

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

in Javascript, try this:

alert(document.getElementById(myTxt).value);
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52