470,612 Members | 2,522 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,612 developers. It's quick & easy.

access dynamic controls in codebehind

10
im creating controls using javascript, and i would like to retrieve the values of the controls in code behind(vb.net/c#) . My vb.net below is not returning them,
Odata is a div but i added the runat = "server" property. Would anyone know what im doing wrong.
Expand|Select|Wrap|Line Numbers
  1. Dim ctr As System.Web.UI.Control '  Web.System.Web.UI.Control
  2.         For Each ctr In oData.Controls
  3.             Windows.Forms.MessageBox.Show(ctr.ID)
  4.         Next ctr
and the javascript code below.


Expand|Select|Wrap|Line Numbers
  1. function LoadControls(strParams)
  2. {       
  3.         //var arr = [];
  4.         //arr = strParams.split(",");
  5.       //var strParams = '<%=strParams%>';
  6.      // alert(strParams);
  7.       var MyHeight;
  8.       MyHeight = 0;
  9.       var di =document.getElementById('oData');
  10.       var SimpleString = strParams; 
  11.       var myArray = [];
  12.       myArray = SimpleString.split(",");
  13.       for (var i = 0; i < myArray.length; i++) 
  14.       {
  15.             var str = myArray[i];
  16.             var j = new Array(2);
  17.             j = str.split("-");
  18.  
  19.             if (j[1] == "Integer")
  20.             {
  21.               var txt5   = document.createElement("input");
  22.               txt5.setAttribute ('type', 'text');
  23.               txt5.setAttribute ('id', j[0]);
  24.               txt5.setAttribute ('name', j[0]);
  25.               txt5.setAttribute ('text', j[0]);
  26.               var chk1 = document.createElement("input");
  27.               chk1.setAttribute ('type', 'checkbox');
  28.               chk1.setAttribute ('id', "chk" + j[0] );
  29.               chk1.setAttribute ('name',"chk" +  j[0]);
  30.               chk1.setAttribute ('text',"chk" +  j[0]); 
  31.               //chk1.innerHTML  = "Default";
  32.               var lbl1   = document.createElement('label');
  33.               lbl1.setAttribute ('id', "lblDefault" );
  34.               lbl1.innerHTML =  "Default";
  35.               lbl1.style.width = "30px";
  36.               var lbl2   = document.createElement('label');
  37.               lbl2.setAttribute ('id', "lblNull");
  38.               lbl2.innerHTML =  "Nulls";
  39.               lbl2.style.width = "30px";
  40.               var lbl3   = document.createElement('label');
  41.               lbl3.setAttribute ('id', "lbl" + j[0]);
  42.               lbl3.innerHTML =  j[0];
  43.               lbl3.style.width = "125px";
  44.               var chk2   = document.createElement("input");
  45.               chk2.setAttribute ('type', 'checkbox');
  46.               chk2.setAttribute ('id', "chkNull" );
  47.               chk2.setAttribute ('name',"chkNull");
  48.               chk2.setAttribute ('text',"Null");
  49.               //chk2.innerHTML  = "null";
  50.               //txt5.value = j[0];
  51.               txt5.style.top='15px';
  52.               txt5.style.left='15px';
  53.               var newdiv = document.createElement('div');
  54.               newdiv.setAttribute('id',"divIdName");                            
  55.               newdiv.appendChild(lbl3);
  56.               newdiv.appendChild(txt5);
  57.               newdiv.appendChild(chk2);              
  58.               newdiv.appendChild(lbl2);
  59.               newdiv.appendChild(chk1);
  60.               newdiv.appendChild(lbl1);
  61.               di.appendChild(newdiv);
  62.              }
  63.             else if (j[1] == "Boolean")
  64.             {
  65.               var txt1   = document.createElement("input");
  66.               txt1.setAttribute ('type', 'radio');
  67.               txt1.setAttribute ('id', j[0]);
  68.               txt1.setAttribute ('name',j[0]);
  69.               txt1.setAttribute ('text',j[0]);
  70.               txt1.value = j[0];
  71.               //txt1.innerText = j[0];
  72.               txt1.style.top= MyHeight + 'px'; // '15px';
  73.               txt1.style.left='0px';
  74.               //rbtnfalse
  75.               var rbtn1   = document.createElement("input");
  76.               rbtn1.setAttribute ('type', 'radio');
  77.               rbtn1.setAttribute ('id',"rbtnF" +  j[0]);
  78.               rbtn1.setAttribute ('name',"rbtnF" + j[0]);
  79.               rbtn1.setAttribute ('text',"rbtnF" + j[0]);
  80.               rbtn1.value = j[0];
  81.                //chkNull
  82.               var chkNull   = document.createElement("input");
  83.               chkNull.setAttribute ('type', 'checkbox');
  84.               chkNull.setAttribute ('id',"chkNull" +  j[0]);
  85.               chkNull.setAttribute ('name',"chkNull" + j[0]);
  86.               chkNull.setAttribute ('text',"chkNull" + j[0]);
  87.              //chkDefault
  88.               var chkDefault   = document.createElement("input");
  89.               chkDefault.setAttribute ('type', 'checkbox');
  90.               chkDefault.setAttribute ('id',"chkD" +  j[0]);
  91.               chkDefault.setAttribute ('name',"chkD" + j[0]);
  92.               chkDefault.setAttribute ('text',"chkD" + j[0]);
  93.               var lbl   = document.createElement('label');
  94.               lbl.setAttribute ('id', "lbl" + j[0]);
  95.               lbl.innerHTML =  j[0];
  96.               lbl.style.width = "120px";
  97.               lbl.style.weight = "bold";
  98.               var lbl1   = document.createElement('label');
  99.               lbl1.setAttribute ('id', "lblTrue" + j[0]);
  100.               lbl1.innerHTML =  "True";
  101.               lbl1.style.width = "50px";
  102.               lbl1.style.weight = "bold";
  103.               var lbl2   = document.createElement('label');
  104.               lbl2.setAttribute ('id', "lblFalse" + j[0]);
  105.               lbl2.innerHTML =  "False";
  106.               lbl2.style.width = "50px";
  107.               lbl2.style.weight = "bold";
  108.               var lbl4   = document.createElement('label');
  109.               lbl4.setAttribute ('id', "lblD" + j[0]);
  110.               lbl4.innerHTML =  "Default";
  111.               lbl4.style.width = "50px";
  112.               lbl4.style.weight = "bold";
  113.               var lbl5   = document.createElement('label');
  114.               lbl5.setAttribute ('id', "lblN" + j[0]);
  115.               lbl5.innerHTML =  "Null";
  116.               lbl5.style.width = "50px";
  117.               lbl5.style.weight = "bold";
  118.               //txt1.style.width='10px';
  119.               //txt1.style.height='20px';
  120.               //txt5.style.visibility = 'visible'; // default anyway
  121.               var newdiv = document.createElement('div');
  122.               newdiv.setAttribute('id',"divIdName");              
  123.               newdiv.appendChild(lbl);
  124.               newdiv.appendChild(txt1);
  125.               newdiv.appendChild(lbl1);
  126.               newdiv.appendChild(rbtn1);                            
  127.               newdiv.appendChild(lbl2);  
  128.               newdiv.appendChild(chkNull);
  129.               newdiv.appendChild(lbl5);
  130.               newdiv.appendChild(chkDefault);
  131.               newdiv.appendChild(lbl4);            
  132.               di.appendChild(newdiv);
  133.               txt1.onclick=function() { if (this.checked==true) { this.checked = false} else {this.checked =true} }
  134.  
  135.             }
  136.       }
Mar 19 '09 #1
4 4527
omlac
10
im creating controls using javascript, and i would like to retrieve the values of the controls in code behind(vb.net/c#) . My vb.net below is not returning them,
Odata is a div but i added the runat = "server" property. Would anyone know what im doing wrong.
Expand|Select|Wrap|Line Numbers
  1. Dim ctr As System.Web.UI.Control '  Web.System.Web.UI.Control
  2.         For Each ctr In oData.Controls
  3.             Windows.Forms.MessageBox.Show(ctr.ID)
  4.         Next ctr
and the javascript code below.


Expand|Select|Wrap|Line Numbers
  1. function LoadControls(strParams)
  2. {       
  3.         //var arr = [];
  4.         //arr = strParams.split(",");
  5.       //var strParams = '<%=strParams%>';
  6.      // alert(strParams);
  7.       var MyHeight;
  8.       MyHeight = 0;
  9.       var di =document.getElementById('oData');
  10.       var SimpleString = strParams; 
  11.       var myArray = [];
  12.       myArray = SimpleString.split(",");
  13.       for (var i = 0; i < myArray.length; i++) 
  14.       {
  15.             var str = myArray[i];
  16.             var j = new Array(2);
  17.             j = str.split("-");
  18.  
  19.             if (j[1] == "Integer")
  20.             {
  21.               var txt5   = document.createElement("input");
  22.               txt5.setAttribute ('type', 'text');
  23.               txt5.setAttribute ('id', j[0]);
  24.               txt5.setAttribute ('name', j[0]);
  25.               txt5.setAttribute ('text', j[0]);
  26.               var chk1 = document.createElement("input");
  27.               chk1.setAttribute ('type', 'checkbox');
  28.               chk1.setAttribute ('id', "chk" + j[0] );
  29.               chk1.setAttribute ('name',"chk" +  j[0]);
  30.               chk1.setAttribute ('text',"chk" +  j[0]); 
  31.               //chk1.innerHTML  = "Default";
  32.               var lbl1   = document.createElement('label');
  33.               lbl1.setAttribute ('id', "lblDefault" );
  34.               lbl1.innerHTML =  "Default";
  35.               lbl1.style.width = "30px";
  36.               var lbl2   = document.createElement('label');
  37.               lbl2.setAttribute ('id', "lblNull");
  38.               lbl2.innerHTML =  "Nulls";
  39.               lbl2.style.width = "30px";
  40.               var lbl3   = document.createElement('label');
  41.               lbl3.setAttribute ('id', "lbl" + j[0]);
  42.               lbl3.innerHTML =  j[0];
  43.               lbl3.style.width = "125px";
  44.               var chk2   = document.createElement("input");
  45.               chk2.setAttribute ('type', 'checkbox');
  46.               chk2.setAttribute ('id', "chkNull" );
  47.               chk2.setAttribute ('name',"chkNull");
  48.               chk2.setAttribute ('text',"Null");
  49.               //chk2.innerHTML  = "null";
  50.               //txt5.value = j[0];
  51.               txt5.style.top='15px';
  52.               txt5.style.left='15px';
  53.               var newdiv = document.createElement('div');
  54.               newdiv.setAttribute('id',"divIdName");                            
  55.               newdiv.appendChild(lbl3);
  56.               newdiv.appendChild(txt5);
  57.               newdiv.appendChild(chk2);              
  58.               newdiv.appendChild(lbl2);
  59.               newdiv.appendChild(chk1);
  60.               newdiv.appendChild(lbl1);
  61.               di.appendChild(newdiv);
  62.              }
  63.             else if (j[1] == "Boolean")
  64.             {
  65.               var txt1   = document.createElement("input");
  66.               txt1.setAttribute ('type', 'radio');
  67.               txt1.setAttribute ('id', j[0]);
  68.               txt1.setAttribute ('name',j[0]);
  69.               txt1.setAttribute ('text',j[0]);
  70.               txt1.value = j[0];
  71.               //txt1.innerText = j[0];
  72.               txt1.style.top= MyHeight + 'px'; // '15px';
  73.               txt1.style.left='0px';
  74.               //rbtnfalse
  75.               var rbtn1   = document.createElement("input");
  76.               rbtn1.setAttribute ('type', 'radio');
  77.               rbtn1.setAttribute ('id',"rbtnF" +  j[0]);
  78.               rbtn1.setAttribute ('name',"rbtnF" + j[0]);
  79.               rbtn1.setAttribute ('text',"rbtnF" + j[0]);
  80.               rbtn1.value = j[0];
  81.                //chkNull
  82.               var chkNull   = document.createElement("input");
  83.               chkNull.setAttribute ('type', 'checkbox');
  84.               chkNull.setAttribute ('id',"chkNull" +  j[0]);
  85.               chkNull.setAttribute ('name',"chkNull" + j[0]);
  86.               chkNull.setAttribute ('text',"chkNull" + j[0]);
  87.              //chkDefault
  88.               var chkDefault   = document.createElement("input");
  89.               chkDefault.setAttribute ('type', 'checkbox');
  90.               chkDefault.setAttribute ('id',"chkD" +  j[0]);
  91.               chkDefault.setAttribute ('name',"chkD" + j[0]);
  92.               chkDefault.setAttribute ('text',"chkD" + j[0]);
  93.               var lbl   = document.createElement('label');
  94.               lbl.setAttribute ('id', "lbl" + j[0]);
  95.               lbl.innerHTML =  j[0];
  96.               lbl.style.width = "120px";
  97.               lbl.style.weight = "bold";
  98.               var lbl1   = document.createElement('label');
  99.               lbl1.setAttribute ('id', "lblTrue" + j[0]);
  100.               lbl1.innerHTML =  "True";
  101.               lbl1.style.width = "50px";
  102.               lbl1.style.weight = "bold";
  103.               var lbl2   = document.createElement('label');
  104.               lbl2.setAttribute ('id', "lblFalse" + j[0]);
  105.               lbl2.innerHTML =  "False";
  106.               lbl2.style.width = "50px";
  107.               lbl2.style.weight = "bold";
  108.               var lbl4   = document.createElement('label');
  109.               lbl4.setAttribute ('id', "lblD" + j[0]);
  110.               lbl4.innerHTML =  "Default";
  111.               lbl4.style.width = "50px";
  112.               lbl4.style.weight = "bold";
  113.               var lbl5   = document.createElement('label');
  114.               lbl5.setAttribute ('id', "lblN" + j[0]);
  115.               lbl5.innerHTML =  "Null";
  116.               lbl5.style.width = "50px";
  117.               lbl5.style.weight = "bold";
  118.               //txt1.style.width='10px';
  119.               //txt1.style.height='20px';
  120.               //txt5.style.visibility = 'visible'; // default anyway
  121.               var newdiv = document.createElement('div');
  122.               newdiv.setAttribute('id',"divIdName");              
  123.               newdiv.appendChild(lbl);
  124.               newdiv.appendChild(txt1);
  125.               newdiv.appendChild(lbl1);
  126.               newdiv.appendChild(rbtn1);                            
  127.               newdiv.appendChild(lbl2);  
  128.               newdiv.appendChild(chkNull);
  129.               newdiv.appendChild(lbl5);
  130.               newdiv.appendChild(chkDefault);
  131.               newdiv.appendChild(lbl4);            
  132.               di.appendChild(newdiv);
  133.               txt1.onclick=function() { if (this.checked==true) { this.checked = false} else {this.checked =true} }
  134.  
  135.             }
  136.       }
Mar 19 '09 #2
acoder
16,027 Expert Mod 8TB
I think you'll have better luck asking the .NET experts - moved to ASP.NET.

PS. please use [code] tags when posting code. Thanks.
Mar 19 '09 #3
@omlac
I have not used VB in a while, but I dont think the above will work as forms cannot be used on a web program. If anything, the message box would pop up on the server and not on the clients browser. I think you can use Response.Write (ctr.ID or whatever) in the code behind. You can copy the code into your aspx page and use "Alert(ctl.ID)".
Mar 19 '09 #4
Frinavale
9,735 Expert Mod 8TB
What are you trying to do here:
Expand|Select|Wrap|Line Numbers
  1.     Windows.Forms.MessageBox.Show(ctr.ID)
This will not work. It will attempt to display a message box on the server but of course there is no way for the application to do this so...What do you want to do?

You should consider changing your <div> into a Panel.
A Panel is rendered as a <div> in the web browser...

However I don't know how this is going to help you in the long run. It looks like you are dynamically creating HTML input elements using JavaScript.

These will not be accessible in your server code.

You should probably check out this article on how to use dynamic controls in asp.net....
Mar 19 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Pat Sagaser via .NET 247 | last post: by
3 posts views Thread by Mr Newbie | last post: by
5 posts views Thread by c676228 | last post: by
4 posts views Thread by Boris Yeltsin | last post: by
2 posts views Thread by WolfyUK | last post: by
2 posts views Thread by germ | last post: by
1 post views Thread by =?Utf-8?B?bWFya203NQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.