触发服务器端事件窗体子页面



我遇到了一个问题,我需要触发服务器端事件

我有一个带有索引的页面。aspx,在同一页面下,我有一些链接标签,如果你点击链接,它会调用另一个页面的DisplayContent。

我想要一些服务器端事件从DisplayContent。aspx使用按钮,如果我点击按钮,它是击中父页索引。你知道为什么会这样吗?

<script>
(function(){with (this[2]) {with (this[1]) {with (this[0]){return function(event) {javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$FragmentContent$btnSend$field&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))
</script>

我认为问题是呼叫"WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions"

您可以使用WebMethod来这样做。按照以下步骤操作

1)创建一个新的页面,并在其中添加脚本管理器(检查它是否已经添加在母版页)

<asp:ScriptManager runat="server" EnablePageMethods="True">
2)使用注释 添加服务器端函数
 [WebMethod]
    public static string callFunction(string words)
    {
        return "Your string was '" + words + "'";
    }

3)在你的aspx页面添加以下html控件

<div>
        <asp:TextBox ID="txtWords" runat="server" ClientIDMode="Static"></asp:TextBox>
        <asp:Button ID="btnCall" OnClientClick="callServerFunction();return false;"
            Text="Submit" runat="server" />
    </div>
4)添加javascript函数来调用这个函数
<script type="text/javascript">
        function callServerFunction() {
            var txtWords = document.getElementById('txtWords').value;
            PageMethods.callFunction(txtWords, OnSuccess, OnFailure);
        }
        //prompt success message..
        function OnSuccess(result) {
            if (result) {
                alert(result);
            }
        }
        //prompt error message..
        function OnFailure(error) {
            if (error) {
                alert(error);
            }
        }
    </script>

在单独的页面中尝试,然后根据您的需要进行修改。

最新更新