简体   繁体   English

从javascript调用asp.net codebehind函数

[英]calling asp.net codebehind function from javascript

I have custom control called LinkControl : 我有一个名为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. 该控件用于转发器内的Page Bookmarks,放在转发器内的每个控件具有不同的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. 问题是,当我按btnCheck按钮以查看Repeater内的任何控件时,当我在CodebehindCheckImage函数中进行调试时,我总是会得到包含在Repeater中的最后一个控件中的字符串lblBookmarkId.Text。

Any suggestions would be welcome. 欢迎大家提出意见。

Regards Wojciech 关于Wojciech

you can modified your javascript function like below; 您可以像下面那样修改您的javascript函数;

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

    }

& change code in your repeater to attach onclientClick like below 并在中继器中更改代码以附加到onclientClick,如下所示

<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. 其中“ BookMarkText”与您使用书签标签绑定的内容相同。 so no need to use hidden bookmark label also. 因此也无需使用隐藏的书签标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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