将数据绑定到 reapeator 控件时,会触发 reapeator 控件内链接按钮的 onclick 事件

发布于 2024-12-09 10:00:24 字数 890 浏览 1 评论 0 原文

我在 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

I have a Linkbutton inside a Repeater control.
My code in the aspx page :

    <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>

Then on the code behind

protected string lnk_OpenSupplement(object profileId)
        {
            string retStr = "success";
                  .........
            return retStr;

        }

In the page_load :

  repeater.DataSource = recentAssignments;
                    repeater.DataBind();

The strange thing happening here is : in the Repeator's databind the lnk_OpenSupplement method gets fired, which is unwanted functionality. How can i avoid this. or can some body point out where am I going wrong.

Thanks in advance

BB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

书间行客 2024-12-16 10:00:24

您将该方法的输出数据绑定到 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

连接事件后,您的按钮将变为:

 <asp:LinkButton ID="lnkBtnOpenSuplmnt" runat="server"  
      Text= "OpensupLink"
      OnClientClick='<%# Eval("ClaimId", "return confirm(\"Reopen the assignment for claim {0} to issue a supplement?\")" ) %>' 
      CommandName="MyCommand" 
      CommandArgument='<%# Eval("ClaimId") %>'>
</asp:LinkButton>

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:

 <asp:LinkButton ID="lnkBtnOpenSuplmnt" runat="server"  
      Text= "OpensupLink"
      OnClientClick='<%# Eval("ClaimId", "return confirm(\"Reopen the assignment for claim {0} to issue a supplement?\")" ) %>' 
      CommandName="MyCommand" 
      CommandArgument='<%# Eval("ClaimId") %>'>
</asp:LinkButton>
哥,最终变帅啦 2024-12-16 10:00:24

我认为 OnClick 的 <%# 标签正在评估数据绑定上的功能,尝试删除它并将其更改为:

  OnClick ='lnk_OpenSupplement'

您还必须在服务器端计算出“ClaimId”,但您应该能够这样做使用将传递到 lnk_OpenSupplement 的标准事件参数。

I think the <%# tag for the OnClick is evaluating the function on databind, try removing that and changing it to:

  OnClick ='lnk_OpenSupplement'

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.

温柔女人霸气范 2024-12-16 10:00:24

罪魁祸首是以下行:

OnClick ='<%# lnk_OpenSupplement(Eval("ClaimId"))%>'>

<%# ... %> 标记将在数据绑定期间被触发。实际上,代码正在执行的操作是在数据绑定上执行 lnk_OpenSupplement,并将其返回值分配给 OnClick

The culprit is the following line:

OnClick ='<%# lnk_OpenSupplement(Eval("ClaimId"))%>'>

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 to OnClick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文