如何为控件创建动态ID [英] How to create dynamic id to the controls

查看:75
本文介绍了如何为控件创建动态ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

谁能帮我如何为控件生成动态ID.


这是我的示例代码....在此先谢谢..


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''/>";


              }

推荐答案

这并不困难.
您已经在通过代码构建控件.

看下面的行-
newcell.innerHTML = " <div id=''hiddenpannel1'' style

除了使用hiddenpabnnel1,您还可以执行此操作
newcell.innerHTML = " <div id=''" + GetDynamicName() + "'' style

GetDynamicName()将使用某种逻辑为您获取名称.
很简单,您可以在方法内部保留一个计数器,并在每次生成新名称时保持其递增.因此,您将获得类似hiddenpannel1, hiddenpannel2, hiddenpannel3的名称.

您可以做更多的事情来创建动态控件(由控件类型等生成的名称),但我将留给您尝试.
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.


使用占位符控件或面板,您可以根据您的要求创建动态控件(例如ID).


标签l;
文本框t;
for(int i = 0; i< 2; i ++)
{
l = new lable();
l.id = i.tostirng();
...
t =新文本框;
t.id = i.tostring();
....
}
placeholder1.controls.Add(l);
placeholder1.controls.Add(t);
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);


这篇关于如何为控件创建动态ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆