onclick event of the link button inside the reapeator control fires at the time of binding data to reapeator control
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 a开发者_如何学Cm I going wrong.
Thanks in advance
BB
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>
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.
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
.
精彩评论