【问题标题】:JavaScript function succeeds but Code behind is not calledJavaScript 函数成功,但未调用后面的代码
【发布时间】:2016-05-14 04:24:57
【问题描述】:

我的 ASP 站点中的代码隐藏和 JavaScript 有一个问题。

我了解了我公司的活动概览,当您单击“详细信息”按钮时,它应该会进入数据库并收集有关该特定活动的所有可用数据,然后将其显示在 Bootstrap 模式窗口中。

唯一的问题是,目前唯一发生的事情是显示了模态窗口,但没有别的。甚至没有调用 Code-Behind。

这是我的 JavaScript:

function GetFullActivityDescription(id) {
    var onSuccess = function () {
        $('#full-activity-description-modal').modal('show');
    }
    var onFailed = function () { }
    var UserContext = this;
    PageMethods.GetFullActivityDescription(id, onSuccess, onFailed, UserContext);
}

这里是 C#:

[WebMethod]
public static void GetFullActivityDescription(Int32 id)
{
    mHeader.InnerHtml = "<h1>Test</h1>";
    mBody.InnerHtml = id + "";
}

我必须进入运行全局元素的部分类并将其默认声明更改为静态声明,以便我可以从 C# 操作元素:

/// <summary>
/// modal_header control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
public static System.Web.UI.HtmlControls.HtmlGenericControl modal_header;

/// <summary>
/// modal_body control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
public static System.Web.UI.HtmlControls.HtmlGenericControl modal_body;

这是 HTML:

<div id="full-activity-description-modal" class="modal fade" role="dialog">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header" runat="server" id="modal_header"></div>
            <div class="modal-body" runat="server" id="modal_body"></div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger btn-bg" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

我需要这样做有点复杂,因为我需要 ASP提交它所在的表单。否则它有点毫无意义,因为模态窗口将被重置。

现在“难题”的最后一部分是描述按钮的生成方式:

html.Append("<td>");
html.Append("<a onclick=\"GetFullActivityDescription(" + id + ");\" class=\"btn\" style=\"background-color: #F3F3F3; color: #000000; font-size: 18px;\">");
html.Append("<span class=\"ion ion-clipboard\"></span>");
html.Append("</a>");
html.Append("</td>");
html.Append("</tr>");

这被添加到活动表中的每个条目中,以便将正确的 id 传递给 JavaScript 函数。你最终会得到这个:

所以只是陈述一个问题:为什么我的 JavaScript 函数被调用但我的 C# 代码隐藏却没有?

编辑 1

这里是通过 Site.Master 添加的 ScriptManager,该页面继承自。

<asp:ScriptManager runat="server" EnablePageMethods="true" EnablePartialRendering="true">
    <Scripts>
        <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%>
        <%--Framework Scripts--%>
        <asp:ScriptReference Name="MsAjaxBundle" />
        <asp:ScriptReference Name="bootstrap" />
        <asp:ScriptReference Name="respond" />
        <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
        <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
        <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
        <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
        <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
        <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
        <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
        <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
        <asp:ScriptReference Name="WebFormsBundle" />
        <%--Site Scripts--%>
    </Scripts>
</asp:ScriptManager>

【问题讨论】:

  • 您是否在 aspx 页面中添加了脚本管理器?
  • 是的,页面继承自 Site.Master
  • 你检查过this post吗?
  • @Zippy 如果我删除该行,那么&lt;webopt:bundlereference runat="server" path="~/Content/css" /&gt; 将停止工作。 webopt 在里面。
  • 尝试检查响应,例如:var result = PageMethods.GetFullActivityDescription(id, function (response) { alert(response); });

标签: javascript c# jquery asp.net twitter-bootstrap


【解决方案1】:

在您的 C# 代码中,尝试ScriptManager.RegisterStartupScript(yourcontrol,this.getType(),"theNameOfYourJSFunction", "theNameOfYourJSFunction( + valueToPass + )", True)。您还需要将 C# 代码放入某种绑定到控件的事件中,以便调用 C# 代码。就目前而言,服务器端没有调用您编写的 C# 代码的任何内容。例如,在Page_Load 函数中调用GetFullActivityDescription

【讨论】:

  • 如果我按照您的建议进行调用,那么在 Page_Load 上调用 javascript,这并不是我真正想要的。 PageMethods.GetFullActivityDescription(JSON.stringify(arr), onSuccess(), onFailed(), UserContext); 这一行也调用了我的服务器端方法。
【解决方案2】:

我认为您只需要更改 Javascript 和代码隐藏。从您的代码中,我可以看到您正在使用 JQuery,我也使用 JQuery 来处理 Modal。

JavaScript

function GetFullActivityDescription(id) {
    $.get("PageName.aspx/MethodName",{ id: id }, function(data) {

        $('#full-activity-description-modal').modal('show');

    });
}

C#

[WebMethod]
public static void GetFullActivityDescription(Int32 id)
{
    mHeader.InnerHtml = "<h1>Test</h1>";
    mBody.InnerHtml = id + "";
}

【讨论】:

  • 如何在静态方法中调用“响应”?这是不可能的。
  • 是的,之前忽略了这一点,抱歉,javascript 现在可以触发该方法
  • 不,它根本不运行,因为我无法用你当前的解决方案编译它。
  • 我又修改了...现在怎么样
  • 不,它不符合我的要求,抱歉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多