asp.NET:如何访问中继器生成的元素? [英] asp.NET: How to access repeater generated elements?
问题描述
我有一个中继器,而不是创建一个表:
i have a repeater than creates a table:
<itemtemplate>
<tr id="theTableRow" runat="server">
<td>
<asp:LinkButton runat="server"
OnClientClick="todo"
Text="Do Something" />
</td>
</tr>
</itemtemplate>
注意: OnClientClick ="todo"
行.
在最终呈现的代码中,我希望 todo 包含对javascript函数的调用,并传递:
In the final rendered code, i want the todo to contain a call to a javascript function, passing:
- 生成的表行的ID和
- 当前绑定对象的属性的评估
现在提供一些伪代码:
伪代码1:
OnClientClick ="DoSomething(theTableRow,CromulentGuid);返回false;"
OnClientClick="DoSomething(theTableRow, CromulentGuid); return false;"
伪代码2
OnClientClick ="javascript:DoSomething(theTableRow,CromulentGuid);返回false;"
OnClientClick="javascript:DoSomething(theTableRow, CromulentGuid); return false;"
伪代码3
OnClientClick ="javascript:DoSomething(theTableRow,<%#Eval(" CromulentGuid)%>);返回false;"
OnClientClick="javascript:DoSomething(theTableRow, <%# Eval("CromulentGuid") %>); return false;"
伪代码4
OnClientClick ="javascript:DoSomething(<%= theTableRow%>,<%#Eval(" CromulentGuid)%>);返回false;"
OnClientClick="javascript:DoSomething(<%= theTableRow %>, <%# Eval("CromulentGuid") %>); return false;"
伪代码5
OnClientClick ='javascript:DoSomething(<%= Eval(theTableRow)%>,<%#Eval("CromulentGuid")%>);返回false;'
OnClientClick='javascript:DoSomething(<%= Eval(theTableRow) %>, <%# Eval("CromulentGuid") %>); return false;'
无论使用什么ASP.NET代码,我都希望呈现的HTML为:
Whatever the ASP.NET code used, i want the rendered HTML to be:
<tr id="ctl00__itemRepeater_ctl01_theTableRow">
<td>
<a
onclick="DoSomething('ctl00__itemRepeater_ctl01_theTableRow', '19a149db-5675-4eee-835d-3d78372ca6f9'); return false;"
href="javascript:__doPostBack('ctl00$itemRepeater$ctl01$ctl04','')">
Do Something
</a>
</td>
</tr>
我也可以:
<tr id="ctl00__itemRepeater_ctl01_theTableRow">
<td>
<a
onclick='DoSomething("ctl00__itemRepeater_ctl01_theTableRow", "19a149db-5675-4eee-835d-3d78372ca6f9"); return false;'
href="javascript:__doPostBack('ctl00$itemRepeater$ctl01$ctl04','')">
Do Something
</a>
</td>
</tr>
注意:我对第二种形式还可以,因为我知道第二种形式在功能上是相同的,并且即使后者的可读性较差,ASP.NET代码也无法生成前一种形式.
Note: i'm okay with the 2nd form since i know it is functionally identical, and ASP.NET code cannot generate the former, even if the latter is less readable.
相关问题:
ASP.NET:如何访问生成的转发器javascript中的元素?
推荐答案
您可以使用 OnItemDataBound
事件更改代码中的每个元素.由于您对HTML特别关注,因此我可能还建议您使用混合控件而不是asp控件.例如:
You can use the OnItemDataBound
event to alter each element in your code. Since you're particular about the HTML I might also recommend using hybrid controls rather than asp controls. For example:
<itemtemplate>
<tr id="theTableRow" runat="server">
<td>
<a runat="server"
onclick="todo(this.parent.parent, '<%# Eval("Property") %>');return false;" >
Do Something
</a>
</td>
</tr>
</itemtemplate>
这可能不是100%完美的,因为我只是直接在回复窗口中键入它,而且我总是一开始就搞砸Eval()语法,但这应该会有所帮助.
This probably isn't 100% perfect, as I just typed it directly in the reply window and I always screw up the Eval() syntax on my first go, but it should help some.
这篇关于asp.NET:如何访问中继器生成的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!