简体   繁体   中英

calling asp.net codebehind function from javascript

I have custom control called LinkControl :

<asp:Panel runat="server">
<script language="javascript" type="text/javascript">
    function CheckImage() {
        var str = document.getElementById('<%=lblBookmarkId.ClientID%>').firstChild.nodeValue;
        PageMethods.CodebehindCheckImage(str);
        return false;
    }
</script>
<asp:Label runat="server" ID="lblBookmarkId" Style="visibility: hidden;" />
<asp:Button runat="server" ID="btnCheck" Text="Check" OnClientClick="return CheckImage();"                                        CausesValidation="false""/>
</asp:Panel>

That control is used on Page Bookmarks inside repeater, each control put inside repeater has diffrent value of lblBookmarkId.Text.

Codebehind page Bookmarks has function :

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static bool CodebehindCheckImage(string str)
{
   return true;
}

The problem is that when I press button btnCheck for any of the controls inside Repeater, when I debug in function CodebehindCheckImage I always get string lblBookmarkId.Text contained in last control that is present in Repeater.

Any suggestions would be welcome.

Regards Wojciech

you can modified your javascript function like below;

    function CheckImage(str) {
        PageMethods.CodebehindCheckImage(str);
        return false;

    }

& change code in your repeater to attach onclientClick like below

<asp:Button runat="server" ID="btnCheck" Text="Check" 
 OnClientClick='<%# Eval("BookMarkText", "return CheckImage(\"{0}\");") >

where "BookMarkText" is same what you are binding with bookmark label. so no need to use hidden bookmark label also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM