中继器"onclick"中的LinkButton将值分配给隐藏的输入 [英] LinkButton in a repeater 'onclick' assigns a value to a hidden input
问题描述
我在转发器内部有一个 asp:LinkButton
,该转发器具有一个 commandname
,其中包含一个 ID
I have a asp:LinkButton
that's inside a repeater , that has a commandname
containing an ID
<table>
<asp:Repeater ID = "rptr" runat = "server">
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID = "lbtn" runat = "server"
text = '<%#showData(Container.DataItem, "CommonName")%>'
CommandName = '<%#showData(Container.DataItem, "ID")%>'
CommandArgument = '<%#showData(Container.DataItem, "CommonName")%>' />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
此外,我还有一个隐藏的 input
字段:(在转发器之外)
Also, I have a hidden input
field: (outside of the repeater)
<input type = "hidden" id = "ID" runat = "server" />
该转发器填充了许多 asp:LinkButton
,每个转发器都有一个唯一的 ID
,该ID存储在 commandname
中.当单击 asp:LinkButton
时,我希望隐藏的输入值存储已单击的按钮的 commandname
.
The repeater populates many asp:LinkButton
, and each one has a unique ID
that's stored in the commandname
. when a asp:LinkButton
is clicked, I want the hidden input value to store the commandname
of the button that's been clicked.
我知道我必须在 onclick
事件中使用某种javascript函数.但是我无法弄清楚具体的方法.
I know that I have to use some sort of javascript function in the onclick
event. But I am not able to figure out the specific way to do it.
提前谢谢.
推荐答案
您可以在转发器内部执行此操作:
You can do this inside the repeater:
<a href="javascript:fillHiddenField(<%# Container.ItemIndex %>, '<%# Eval("ID").ToString() %>')">Fill field</a>
然后是转发器外部的 HiddenField
和javascript函数来存储值.
And then a HiddenField
and a javascript function outside the repeater to store the value.
<asp:HiddenField ID="HiddenField1" runat="server" />
<script type="text/javascript">
function fillHiddenField(rowNumber, rowValue) {
document.getElementById("<%= HiddenField1.ClientID %>").value = rowValue;
}
现在,您不必进行回传即可在隐藏字段中获取值,但在由另一个元素完成的回传中仍然可用.
Now you won't have to do a postback to get the value in the hidden field but it is still available on postback done by another element.
P.S.最好不要将"ID"用作元素的ID,否则可能会造成混乱.以及=号前后的空格也是如此.
P.S. it's better not to use "ID" as the ID of elements, it could cause confusion. As are the spaces before and after the = signs.
这篇关于中继器"onclick"中的LinkButton将值分配给隐藏的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!