从javascript调用asp.net codebehind函数 [英] calling asp.net codebehind function from javascript

查看:73
本文介绍了从javascript调用asp.net codebehind函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为LinkControl的自定义控件:

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>

控件用于转发器中的页面书签,放在转发器中的每个控件具有lblBookmarkId.Text的不同值。

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;
}

问题是,当我按btnCheck按钮来检查Repeater内的任何控件时,当我在功能CodebehindCheckImage中进行调试时,我总是会得到包含在Repeater中的最后一个控件中的字符串lblBookmarkId.Text。

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.

关于
Wojciech

Regards Wojciech

推荐答案

您可以按以下方式修改javascript函数;

you can modified your javascript function like below;

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

    }

&更改中继器中的代码以附加onclientClick,如下所示:

& change code in your repeater to attach onclientClick like below

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

其中 BookMarkText与您使用书签标签绑定的内容相同。因此也无需使用隐藏的书签标签。

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

这篇关于从javascript调用asp.net codebehind函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆