Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi folks,

Can any one help me how to generate dynamic id's to the controls..


here my sample code.... Thanks in Advance..


 var tbl = document.getElementById('maintable');
var newcell = newRow.insertCell();
    newcell.innerHTML = "<input type='checkbox' name='t1' id='ck1' >";



    newcell = newRow.insertCell();
    newcell.innerHTML = "<input type='text' name='t3'>";

    newcell = newRow.insertCell();
    newcell.innerHTML = " <div id='hiddenpannel1' style='visibility: hidden; width: '100%''><table><tr><td> <select id='hiddenpanel1select' style='visibility: hidden'></select></td></tr><tr><td>  <input type='submit' name='Add' value='Add' style='width: 50px;' onclick='return addFields('hiddenpanel1select','content1')' /></td><td><input type='submit' name='Canel' value='Cancel' style='width: 50px;' /></td></tr></table></div>";


      newcell=newRow.insertCell();
    newcell.innerHTML="<select name='selecttype' id='selecttype' onchange='return show(selecttype,hiddenpannel1)' style='width: 100px;'> <option value='INPUT'>INPUT</option>  <option value='CHECKBOX'>CHECKBOX</option> <option selected='selected' value='SELECT'>SELECT</option> <option value='RADIO'>RADIO</option> </select>";





    newcell=newRow.insertCell();
    newcell.innerHTML="<input type='checkbox' name='t5'/>";


              }
Posted
Updated 18-May-11 18:07pm
v2

It isn't that difficult.
You are already building your controls through code.

Look at the following line -
newcell.innerHTML = " <div id='hiddenpannel1' style

Instead of using hiddenpabnnel1 you can do this
newcell.innerHTML = " <div id='" + GetDynamicName() + "' style

The GetDynamicName() will use some sort of logic to get a name for you.
Very simply, you can keep a counter inside the method and keep incrementing it every time a new name is generated. So, you would get names like hiddenpannel1, hiddenpannel2, hiddenpannel3 returned.

There is lots more you can do to create dynamic control (name generated by control type etc.), but I will leave that for you to try out.
 
Share this answer
 
Comments
hiseshu 19-May-11 0:31am    
Hi abhinav

i am new to this topic .I tried as u suggested but it wont works can u tell me a sample code the get the id's please...


Thanks
Seshu
using placeholder control or panle you can create dynamic controls as per your requirement like id's.


lable l;
textbox t;
for(int i=0;i<2;i++)
{
l=new lable();
l.id=i.tostirng();
...
t=new textbox;
t.id=i.tostring();
....
}
placeholder1.controls.Add(l);
placeholder1.controls.Add(t);
 
Share this answer
 

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