onclick event of the link button inside the reapeator control fires at the time of binding data to reapeator control

BumbleBee picture BumbleBee · Oct 11, 2011 · Viewed 7.9k times · Source

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

Answer

Jim Schubert picture Jim Schubert · Oct 11, 2011

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>