calling asp.net codebehind function from javascript

只愿长相守 提交于 2020-01-06 08:09:49

问题


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:


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.



来源:https://stackoverflow.com/questions/12425797/calling-asp-net-codebehind-function-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!