将数据绑定到 reapeator 控件时,会触发 reapeator 控件内链接按钮的 onclick 事件
我在 Repeater
控件内有一个 Linkbutton
。
我在 aspx 页面中的代码:
<asp:LinkButton ID="lnkBtnOpenSuplmnt" runat="server"
Text= "OpensupLink"
OnClientClick='<%# Eval("ClaimId", "return confirm(\"Reopen the assignment for claim {0} to issue a supplement?\")" ) %>'
OnClick ='<%# lnk_OpenSupplement(Eval("ClaimId"))%>'>
</asp:LinkButton>
后面的代码上
protected string lnk_OpenSupplement(object profileId)
{
string retStr = "success";
.........
return retStr;
}
然后在 page_load
repeater.DataSource = recentAssignments;
repeater.DataBind();
:这里发生的奇怪的事情是:在 Repeator 的数据绑定中,lnk_OpenSupplement
方法被触发,这是不需要的功能。我怎样才能避免这种情况。或者有人可以指出我哪里出错了。
提前谢谢
BB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将该方法的输出数据绑定到
OnClick
事件。意思是,您说的是
OnClick = "success"
,这不是您期望发生的情况。<%# some %>
表示“绑定此元素时执行某些操作并在此处使用返回值”。我建议您看看如何将命令参数绑定到 ItemCommand 事件。
以下是一些描述如何执行此操作的文章:
http:// ondotnet.com/pub/a/dotnet/2003/03/03/repeater.html
http://www.asp.net /data-access/tutorials/custom-buttons-in-the-datalist-and-repeater-vb
连接事件后,您的按钮将变为:
You're databinding the output of that method to the
OnClick
event.Meaning, you're saying
OnClick = "success"
, which isn't what you're expecting to happen.<%# something %>
means 'Execute something when binding this element and use the return value here'.I'd recommend you take a look at how to bind command arguments to the ItemCommand event.
Here are some articles that describe how to do this:
http://ondotnet.com/pub/a/dotnet/2003/03/03/repeater.html
http://www.asp.net/data-access/tutorials/custom-buttons-in-the-datalist-and-repeater-vb
After hooking up the event, your button would then become:
我认为 OnClick 的 <%# 标签正在评估数据绑定上的功能,尝试删除它并将其更改为:
您还必须在服务器端计算出“ClaimId”,但您应该能够这样做使用将传递到 lnk_OpenSupplement 的标准事件参数。
I think the <%# tag for the OnClick is evaluating the function on databind, try removing that and changing it to:
You will have to work out the "ClaimId" on the server side as well, but you should be able to do that using the standard event arguments that would get passed into lnk_OpenSupplement.
罪魁祸首是以下行:
<%# ... %>
标记将在数据绑定期间被触发。实际上,代码正在执行的操作是在数据绑定上执行 lnk_OpenSupplement,并将其返回值分配给OnClick
。The culprit is the following line:
The
<%# ... %>
tag will be triggered during data binding. Effectively what the code is doing is executing lnk_OpenSupplement on data binding, and assigning it's return value toOnClick
.