简体   繁体   English

JavaScript函数成功,但未调用后面的代码

[英]JavaScript function succeeds but Code behind is not called

I have a problem with Code-Behind and JavaScript working together in my ASP site. 我在ASP站点中无法使用Code-Behind和JavaScript一起工作。

I got an overview of activities in my company and when you click a "Detail" button it's supposed to go to a database and collect all the available data about that specific activity and then show it in a Bootstrap Modal window. 我获得了公司活动的概述,当您单击“详细信息”按钮时,它应该进入数据库并收集有关该特定活动的所有可用数据,然后在“ Bootstrap Modal”(引导程序模式)窗口中显示。

The only problem is that currently the only thing that happens is that the modal window is shown but nothing else. 唯一的问题是,当前唯一发生的是显示了模式窗口,但没有其他显示。 The Code-Behind is not even called. 甚至没有调用隐藏代码。

Here is my JavaScript: 这是我的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);
}

And here is the C#: 这是C#:

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

I had to go into the Partial Class where global elements are run and change it's default declaration to a static one so that I could manipulate the elements from C#: 我必须进入运行全局元素的Partial Class,并将其默认声明更改为静态声明,以便可以从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;

Here is the HTML: 这是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>

I need to do it kind of convoluted because I need ASP to NOT submit the form it's in. Otherwise it's kind of pointless as the Modal Window will be reset then. 我需要做一些麻烦的事情,因为我需要ASP 提交它所在的表单。否则,这是毫无意义的,因为此时将重置“模态窗口”。

Now the last piece of the "puzzle" is the way the Description buttons are generated: 现在,“拼图”的最后一部分是描述按钮的生成方式:

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>");

This is added to every entry in the activity table so that the right id is passed to the JavaScript function. 它将添加到活动表的每个条目中,以便将正确的ID传递给JavaScript函数。 You end up with this: 您最终得到以下结果:

在此处输入图片说明

So just to state the question: How come my JavaScript function is called but my C# code-behind is not? 因此,仅提出一个问题:为什么调用我的JavaScript函数,却不调用我的C#代码呢?

EDIT 1 编辑1

Here is the ScriptManager added via the Site.Master which this page inherits from. 这是通过此页面继承的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>

In your C# code, try ScriptManager.RegisterStartupScript(yourcontrol,this.getType(),"theNameOfYourJSFunction", "theNameOfYourJSFunction( + valueToPass + )", True) . 在您的C#代码中,尝试ScriptManager.RegisterStartupScript(yourcontrol,this.getType(),"theNameOfYourJSFunction", "theNameOfYourJSFunction( + valueToPass + )", True) You also need to put your C# code into some kind of event that is bound to a control, so that the C# code is called. 您还需要将C#代码放入绑定到控件的某种事件中,以便调用C#代码。 As it stands, there is nothing on the server side that is calling the C# code you've written. 就目前而言,服务器端没有任何东西可以调用您编写的C#代码。 For example, call GetFullActivityDescription in the Page_Load function. 例如,在Page_Load函数中调用GetFullActivityDescription

I think you only need to change your Javascript and Code-behind. 我认为您只需要更改Javascript和后台代码。 From your code, I can see you're using JQuery, I have use JQuery too to handle the Modal. 从您的代码中,我可以看到您正在使用JQuery,我也使用JQuery来处理Modal。

JavaScript 的JavaScript

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

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

    });
}

C# C#

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

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

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