英文:
How to show/hide modal popup before and after send message to clients using jquery in asp.net c#
问题
I want to show a modal popup message before sending the messages to clients and hide that modal popup after sent messages to clients (show/hide) to achieve this concept I have written below logic.
Note(my logic should work like this):- While sending the messages to clients the modal popup should show like "you will receive messages shortly please wait a moment once done popup will close" and once all messages sent to clients the modal popup should hide.
Modal popup logic:-
<div class="modal fade " id="modal_loader" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-bs-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body">
<div class="form-group row">
<div class="loader"> <img src="assets/images/loding_animation.gif" /></div>
<p> <center>you will receive messages shortly please wait a moment once done popup will close</center></p>
</div>
</div>
</div>
</div>
</div>
HTML logic is:-
<a href="javascript://" title="Export to Whatsapp" id="plrepobulkwhatsapp" onclick="plreportexportexcel_pdftoemail()" class="btn btn-success">Email</a>
And jquery logic is:-
function plreportexportexcel_pdftoemail() {
$("#modal_loader").modal('show');
var msg = Successmessage("abc");
if (msg != "") {
$("#modal_loader").modal('hide');
alert(bulkexcelpdfmsg);
}
}
In Successmessage(code)
method I written the ajax
logic to execute the GenerateCustomPDF_ExcelBulk
method and getting the msg
return data.
aspx.cs:-
[WebMethod]
public static string GenerateCustomPDF_ExcelBulk(string clientcode)
{
string msg = "";
//Here I written my required logic and returned the success message
//msg = "success";
return msg;
}
Here my requirement is if I click the <a>
tag my plreportexportexcel_pdftoemail()
method will execute and should show the modal_loader
popup message and once I receive the msg
response from the backend GenerateCustomPDF_ExcelBulk
method the modal_loader
popup message should hide like this I have written logic but it is not executing properly.
Suggest me where I did the mistake in my code to show/hide the popup msg?
Sorry for my bad English.
New:- Added Successmessage Function Logic :-
function Successmessage(clientcode) {
var sus = "";
var param = { clientcode: clientcode };
var res = $.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "reports.aspx/GenerateCustomPDF_ExcelBulk",
dataType: "json",
data: JSON.stringify(param),
success: function (response) {
sus = response.d;
},
async: false,
error: function (err) {
console.log("error is ==> .. " + err);
}
});
return sus;
}
英文:
I want to show a modal popup message before sending the messages to clients and hide that modal popup after sent messages to clients (show/hide) to achieve this concept I have written below logic.
Note(my logic should work like this):- While sending the messages to clients the modal popup should show like "you will receive messages shortly please wait a moment once done popup will close" and once all messages sent to clients the modal popup should hide.
Modal popup logic:-
<div class="modal fade " id="modal_loader" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-bs-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body">
<div class="form-group row">
<div class="loader"> <img src="assets/images/loding_animation.gif" /></div>
<p> <center>you will receive messages shortly please wait a moment once done popup will close</center></p>
</div>
</div>
</div>
</div>
</div>
HTML logic is:-
<a href="javascript://" title="Export to Whatsapp" id="plrepobulkwhatsapp" onclick="plreportexportexcel_pdftoemail()" class="btn btn-success">Email</a>
And jquery logic is:-
function plreportexportexcel_pdftoemail() {
$("#modal_loader").modal('show');
var msg = Successmessage("abc");
if (msg != "")
{
$("#modal_loader").modal('hide');
alert(bulkexcelpdfmsg);
}
}
In Successmessage(code)
method I written the ajax
logic to execute the GenerateCustomPDF_ExcelBulk
method and getting the msg
return data.
aspx.cs:-
[WebMethod]
public static string GenerateCustomPDF_ExcelBulk(string clientcode)
{
string msg = "";
//Here I written my required logic and returned the success message
//msg = "success";
return msg;
}
Here my requirement is if I click the <a>
tag my plreportexportexcel_pdftoemail()
method will execute and should show the modal_loader
popup message and once I receive the msg
response
from backend GenerateCustomPDF_ExcelBulk
method the modal_loader
popup message should hide like this I have written logic but it is not executing properly.
Suggest me where I did the mistake in my code to show/hide the popup msg?
Sorry for my bad English.
New:- Added Successmessage Function Loigic :-
function Successmessage(clientcode) {
//$("#modal_loader").modal('show');
var sus = "";
var param = { clientcode: clientcode };
var res = $.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "reports.aspx/GenerateCustomPDF_ExcelBulk",
dataType: "json",
//data: "{}",
data: JSON.stringify(param),
success: function (response) {
//$("#modal_loader").modal('hide');
sus = response.d;
},
async: false,
error: function (err) {
console.log("eror is ==> .. " + err);
}
});
return sus;
}
答案1
得分: 1
这是在使用ajax时常见的一个困惑。
JavaScript是异步的(大部分情况下)。
所以,不要试图从"SuccessMessage"函数中返回一个值,而是应该尝试在特定的"回调"函数中执行你的逻辑。
否则,你会得到不希望出现的效果,比如在web请求之后执行"modal('show')"。
我不得不做3个更改才能使你的代码工作:
1- 使用"success"回调。这将在web请求成功完成后执行。请注意,我将其设置为"async:true"。另外,我从这个函数中不返回任何东西。
所以你的代码应该像这样:
function SuccessMessage(clientCode) {
var param = { clientcode: clientCode };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "reports.aspx/GenerateCustomPDF_ExcelBulk",
dataType: "json",
data: JSON.stringify(param),
success: function (response) {
// 在这里执行你的逻辑
if(response !== undefined && response !== null){
var msg = response.d;
if (msg !== undefined && msg !== null && msg.trim() != "") {
console.log("notempty");
// 仅用于演示目的添加了此延迟
setTimeout(function () {
$("#modal_loader").modal('hide');
}, 1000);
} else {
console.log("empty");
}
}
},
async: true,
error: function (err) {
// 错误是一个对象而不是一个字符串
console.log(err);
}
});
}
2- 更改plreportexportexcel_pdftoemail函数。它有一个拼写错误,"Successmessage"应该是"SuccessMessage"......请记住JavaScript是大小写敏感的。
function plreportexportexcel_pdftoemail() {
$("#modal_loader").modal('show');
SuccessMessage("abc");
}
3- 更改C#操作以返回Json,因为你的函数始终返回一个空字符串。如果不返回Json,jQuery Ajax调用将不会命中成功函数,而是会命中错误回调。
public string GenerateCustomPDF_ExcelBulk(string clientcode)
{
string msg = "123ABC";
// 有更好的方式将其转换为json,比如Newtonsoft
return $"{{ \"d\":\"{msg}\" }}";
}
最后,所有这些都执行得非常快,所以如果C#不花时间来处理,看起来会很奇怪......它会非常快地显示和隐藏模态框。所以我添加了一个JavaScript的"延迟",使用setTimeout,仅用于演示目的。你应该根据你的代码选择最合适的方法。
英文:
This is a pretty common confusion when using ajax.
Javascript is async (for the most part).
So instead of trying to return a value from the "SuccessMessage" function, you should instead try to execute your logic in the specific "callback" function.
Otherwise you end up with undesired effects like the "modal('show')" executing after the webrequest.
I had to make 3 changes to make your code work:
1- Use the "success" callback. This will be executed after the web request has successfully completed. Notice I made it "async:true". Also I am not returning anything form this function.
So your code should be like this:
function SuccessMessage(clientCode) {
var param = { clientcode: clientCode };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "reports.aspx/GenerateCustomPDF_ExcelBulk",
dataType: "json",
//data: "{}",
data: JSON.stringify(param),
success: function (response) {
//execute your logic here
if(response !== undefined && response !== null){
var msg = response.d;
if (msg !== undefined && msg !== null && msg.trim() != "") {
console.log("notempty");
//added this delay for demo purposes only
setTimeout(function () {
$("#modal_loader").modal('hide');
}, 1000);
} else {
console.log("empty");
}
}
},
async: true,
error: function (err) {
//error is an object not a string
console.log(err);
}
});
}
2- Change the plreportexportexcel_pdftoemail function. It had a typo "Successmessage" instead of "SuccessMessage"... remember javascript is case sensitive.
function plreportexportexcel_pdftoemail() {
$("#modal_loader").modal('show');
SuccessMessage("abc");
}
3- Change the C# action to return Json because your function is always returning an empty string. If you dont return Json the jQuery Ajax call will not hit the success function and instead hit the error callback.
public string GenerateCustomPDF_ExcelBulk(string clientcode)
{
string msg = "123ABC";
//there are better ways to convert to json like Newtonsoft
return $"{{ \"d\":\"{msg}\" }}";
}
Finally all of this executes real fast so it looks weird if the C# does not take time to process... It would show and hide the modal really fast.
So I added a javascript "delay" using a setTimeout, just for demo purposes. You should do what is best for your code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论