calling asp.net codebehind function from javascrip

2019-09-07 07:59发布

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

1条回答
淡お忘
2楼-- · 2019-09-07 08:18

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.

查看更多
登录 后发表回答