简体   繁体   English

如何动态更改中继器内的控件ID(C#asp.net)?

[英]How to change the id of control within the repeater dynamically (C# asp.net)?

I have linkbutton within the repeater. 我在中继器内有linkbutton。 The ID of the repeater is " lnkbtn ". 转发器的ID为“ lnkbtn ”。

I have a datatable containing a column called as " Questionnumber ". 我有一个数据表,其中包含称为“ Questionnumber ”的列。

The " Questionnumber "(column) contains records such as 1,2,3,16,24.....so on. Questionnumber ”(列)包含诸如1,2,3,16,24 .....等记录。

I want to set the ID of the linkbutton dynamically.This id must be respective to the " Questionnumber ". 我想动态设置linkbutton的ID。此ID必须与“ Questionnumber ”相对应。

if i set ID of the linkbutton(within repeater) as ID='<%# Eval("Questionnumber") %>' , i face problem when i intend to write the coding as repeater1.Items[].Findcontrol("lnkbtn") 如果我将linkbutton(在Repeater中)的ID='<%# Eval("Questionnumber") %>'设置为ID='<%# Eval("Questionnumber") %>' ,则当我打算将编码编写为repeater1.Items[].Findcontrol("lnkbtn")

Why you want to put question number value into link button id? 为什么要将问题编号值放入链接按钮ID? (Note that in templated controls such as repeater, html ids would be anyway different). (请注意,在诸如中继器之类的模板控件中,html id始终是不同的)。 If you need to identify linkbutton for question number then CommandArgument property of linkbutton would be a correct choice. 如果您需要为问题编号标识linkbutton,则linkbutton的CommandArgument属性将是正确的选择。

I've did this for dynamically generated controls. 我这样做是为了动态生成控件。 You can prefix a tag such as LinkButton_X. 您可以在标签前面加上LinkBut​​ton_X。 You can override the ondatabinding for each element and set the id there OR you could use the databinding syntax with a method and a static seed generator, ie a static int that is looped. 您可以覆盖每个元素的ondatabinding并在那里设置id,或者可以将databinding语法与方法和静态种子生成器(即循环的静态int)一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM