【发布时间】: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 如果我删除该行,那么
<webopt:bundlereference runat="server" path="~/Content/css" />将停止工作。 webopt 在里面。 -
尝试检查响应,例如:
var result = PageMethods.GetFullActivityDescription(id, function (response) { alert(response); });
标签: javascript c# jquery asp.net twitter-bootstrap