我发生了一个与以下帖子松散相关的特殊问题:
外部.js文件中的jQuery ajax函数不会调用代码隐藏Webmethod
我从客户端获得了正确的ajax功能。但是,据我所知,WebMethod不是每次都执行,或者我不会一直说。
我注意到在某些情况下,ajax函数将保存我的客户端参数数据,但在某些情况下,后面代码中的WebMethod将不会被调用,程序将直接跳转到OnClick事件这需要WebMethod保存的参数以确保应用程序正常运行。
例如,当对DB进行多次调用时,它将在第一次正确执行插入,但在某些时候可能是第二次,第三次(不一致),存储参数的变量不会因WebMethod而改变没有被ajax函数调用。
似乎每次调用ajax函数,但WebMethod每次都无法执行。
我在下面的代码中提供了样本:
.aspx - 页面中嵌入的Onclick和OnClientClick按钮事件:
<div class="modal fade" id="mdlNewCkLines" data-target=".bs-example-modal-sm" data-keyboard="false" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="newModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close hidden" data-dismiss="modal" aria-hidden="true">x</button>
<center><h4 class="modal-title">New Check</h4></center>
</div>
<div class="modal-body">
<div class="row"> </div>
<asp:Panel runat="server" ID="pnlNewCk2">
<div class="panel panel-info" id="newCheckPanel2">
<div class=" panel-heading">
<h2 class="panel-title"><b>Add Check Lines</b></h2>
</div>
<div class="panel-body" style="background-color: #fbfbfb;">
<div class="form">
<div class="row">
<asp:Table runat="server" ID="tblLines" CssClass="data3">
<asp:TableRow>
<asp:TableCell Width="200">
<asp:Label ID="lblLines" runat="server" Text="CHECK LINE" CssClass="text-primary" style="padding-left:60px;"/>
</asp:TableCell>
<asp:TableCell Width="300">
<asp:DropDownList runat="server" Width="275" CssClass="form-control input-sm" ID="ddlLines" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell Width="200">
<asp:Label ID="lblLinesAmount" runat="server" Text="AMOUNT <b>$</b>" CssClass="text-primary" style="padding-left:60px;"/>
</asp:TableCell>
<asp:TableCell Width="100">
<asp:TextBox CssClass="form-control input-sm" ID="tbLinesAmount" Width="100" runat="server" />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<div class="col-sm-12">
<div class="col-sm-offset-9">
<asp:Button type="button" runat="server" class="btn btn-danger btn-xs" OnClick="btnAddNewCkLine_Click" OnClientClick="getCkLineAmt('tbLinesAmount'); saveDdlLinesParms();" ID="btnAddNewCkLine" Text="Submit" UseSubmitBehavior="false"/> <%--OnClick="btnAddNewCkLine_Click"--%>
</div>
</div>
</div>
</div>
</div>
</div>
</asp:Panel>
</div>
<div class="modal-footer">
<div class="row">
<asp:Button type="button" runat="server" class="btn btn-primary btn-sm" ID="btnSubmitCkLine" OnClientlick="closeModal('mdlCheckLines');" Text="Finish" UseSubmitBehavior="true" />
<asp:Button runat="server" class="btn btn-info btn-sm" data-dismiss="modal" ID="cancelNewCk" OnClick="btnCancelNewCk_Click" Text="Cancel" type="button" UseSubmitBehavior="false"/>
</div>
</div>
</div>
</div>
<div class="modal fade" id="mdlSubmitCkLines" tabindex="-1" data-keyboard="false" data-backdrop="static" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close hidden" data-dismiss="modal" aria-hidden="true">x</button><center><h4 class="modal-title"><b>Check Writing</b></h4></center>
</div>
<div class="modal-body">
<div class="well" id="Div7">
<div class="form">
<div class="row">
<div class="col-sm-12"><center>
<label class="col-sm-12" style="font-size: 12px;"><b>Check Line Submitted!</b><br></label></center>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<asp:Button ID="btnAnotherCkLine" runat="server" OnClick="btnAnotherCkLine_Click" OnClientClick="hideModal('mdlSubmitCkLines');" type="button" CssClass="btn btn-primary btn-sm" Text="Ok" UseSubmitBehavior="false" />
<asp:Button ID="btnFinishNewCk" runat="server" type="button" CssClass="btn btn-primary btn-sm" class="btn btn-primary btn-sm" onClientClick="hideModal('mdlSubmitCkLines'); hideModal('mdlCheckLines');" Text="Finish" UseSubmitBehavior="true"/>
</div>
</div>
</div>
.aspx.cs - 接受.ajax params的WebMethod,包括调用与应用程序的这个特定部分交互的JS函数的Button事件:
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static void SaveCkLinesParms(string lineVal, string lineText, string lineAmt)
{
string ckLineVal = lineVal;
string ckLineTxt = lineText;
string ckLineAmt = lineAmt;
HttpContext.Current.Session["CheckLineVal"] = ckLineVal;
HttpContext.Current.Session["CheckLineTxt"] = ckLineTxt;
HttpContext.Current.Session["CheckLineAmt"] = ckLineAmt;
}
protected void btnAddNewCkLine_Click(object sender, EventArgs e)
{
TreasurersChecksEE TCEE = new TreasurersChecksEE();
TCEE.ckLineVal = (string)HttpContext.Current.Session["CheckLineVal"];
TCEE.ckLineTxt = (string)HttpContext.Current.Session["CheckLineTxt"];
TCEE.issueTypeVal = (string)HttpContext.Current.Session["Issue Type Value"];
TCEE.issueTypeTxt = (string)HttpContext.Current.Session["Issue Type Text"];
TCEE.ckLineAmt = (string)HttpContext.Current.Session["CheckLineAmt"];
TCEE.refundNum = (string)HttpContext.Current.Session["Refund Number"];
InsertCheckDetail(TCEE.refundNum, TCEE.ckLineVal, TCEE.ckLineAmt);
ScriptManager.RegisterStartupScript(this, this.GetType(), "Show Modal Popup", "openModal('mdlSubmitCkLines');", true);
}
protected void btnAnotherCkLine_Click(object sender, EventArgs e)
{
TreasurersChecksEE TCEE = new TreasurersChecksEE();
TCEE.ckLineVal = (string)HttpContext.Current.Session["CheckLineVal"];
TCEE.ckLineTxt = (string)HttpContext.Current.Session["CheckLineTxt"];
TCEE.issueTypeVal = (string)HttpContext.Current.Session["Issue Type Value"];
TCEE.issueTypeTxt = (string)HttpContext.Current.Session["Issue Type Text"];
TCEE.ckLineAmt = (string)HttpContext.Current.Session["CheckLineAmt"];
TCEE.refundNum = (string)HttpContext.Current.Session["Refund Number"];
ScriptManager.RegisterStartupScript(this, this.GetType(), "Show Modal Popup", "setPanel(" + " '" + ddlAdd.ClientID + "', "
+ " '" + nCkPTitle.ClientID + "', "
+ " '" + manCkEntry.ClientID + "', "
+ " '" + nCkLabel.ClientID + "', "
+ " '" + txtRefNo.ClientID + "', "
+ " '" + TCEE.issueTypeVal + "', "
+ " '" + TCEE.issueTypeTxt + "', "
+ " '" + ddlLines.ClientID + "' "
+ "); openModal('mdlNewCkLines');", true);
}
.cs - 数据库调用并插入:
public void InsertCheckDetail(string refNo, string lineVal, string amt)
{
object obj = null;
if (DBC == null)
DBC = new DBConn();
DBC.ExecutePackage("TCMS.TR_CHECK_PROCESSING.DoInsertCheckDetail", out obj, new ParameterDirection[] { ParameterDirection.Input, ParameterDirection.Input, ParameterDirection.Input }, new object[] { refNo, lineVal, amt })
}
.js JS .ajax函数将params发送给代码隐藏:
function saveDdlLinesParms() {
$.ajax({
url: "TreasurersChecksEE.aspx/SaveCkLinesParms",
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ lineVal: ckLineVal, lineText: ckLineTxt, lineAmt: ckLineAmt }),
datatype: "json",
context: document.body,
error: function (xhr, error) {
console.debug(xhr); console.debug(error);
}
// ,success: function (response) {
// response ? alert("No: " + num + " Txt: " + txt + " RefNo: " + refundNo ) : alert("It didn't work.");
// }
})};
对于有关此问题的任何帮助/信息,我将不胜感激。
提前致谢!