How to show/hide modal popup before and after send message to clients using jquery in asp.net c#

huangapple go评论69阅读模式
英文:

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:-

&lt;div class=&quot;modal fade &quot; id=&quot;modal_loader&quot; data-bs-backdrop=&quot;static&quot; data-bs-keyboard=&quot;false&quot; tabindex=&quot;-1&quot; aria-bs-hidden=&quot;true&quot;&gt;
                &lt;div class=&quot;modal-dialog modal-dialog-centered&quot;&gt;
                    &lt;div class=&quot;modal-content&quot;&gt;
                        &lt;div class=&quot;modal-body&quot;&gt;
                            &lt;div class=&quot;form-group row&quot;&gt;
                                &lt;div class=&quot;loader&quot;&gt; &lt;img src=&quot;assets/images/loding_animation.gif&quot; /&gt;&lt;/div&gt;
                                &lt;p&gt; &lt;center&gt;you will receive messages shortly please wait a moment once done popup will close&lt;/center&gt;&lt;/p&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;

HTML logic is:-

&lt;a href=&quot;javascript://&quot; title=&quot;Export to Whatsapp&quot; id=&quot;plrepobulkwhatsapp&quot; onclick=&quot;plreportexportexcel_pdftoemail()&quot; class=&quot;btn btn-success&quot;&gt;Email&lt;/a&gt;

And jquery logic is:-


function plreportexportexcel_pdftoemail() {

$(&quot;#modal_loader&quot;).modal(&#39;show&#39;);

var msg = Successmessage(&quot;abc&quot;);
if (msg != &quot;&quot;)
        {
            $(&quot;#modal_loader&quot;).modal(&#39;hide&#39;);
            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 = &quot;&quot;;

       //Here I written my required logic and returned the success message 
       //msg = &quot;success&quot;;
        return msg;
}

Here my requirement is if I click the &lt;a&gt; 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) {

//$(&quot;#modal_loader&quot;).modal(&#39;show&#39;);

var sus = &quot;&quot;;
var param = { clientcode: clientcode };

var res = $.ajax({

    type: &quot;POST&quot;,
    contentType: &quot;application/json; charset=utf-8&quot;,
    url: &quot;reports.aspx/GenerateCustomPDF_ExcelBulk&quot;,
    dataType: &quot;json&quot;,
    //data: &quot;{}&quot;,
    data: JSON.stringify(param),
    success: function (response) {


        //$(&quot;#modal_loader&quot;).modal(&#39;hide&#39;);

        sus = response.d;

    },
    async: false,
    error: function (err) {
        console.log(&quot;eror is ==&gt; .. &quot; + 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: &quot;POST&quot;,
        contentType: &quot;application/json; charset=utf-8&quot;,
        url: &quot;reports.aspx/GenerateCustomPDF_ExcelBulk&quot;,
        dataType: &quot;json&quot;,
        //data: &quot;{}&quot;,
        data: JSON.stringify(param),
        success: function (response) {

            //execute your logic here
            if(response !== undefined &amp;&amp; response !== null){
                var msg = response.d;

                if (msg !== undefined &amp;&amp; msg !== null &amp;&amp; msg.trim() != &quot;&quot;) {
                    console.log(&quot;notempty&quot;);
                    //added this delay for demo purposes only
                    setTimeout(function () {
                        $(&quot;#modal_loader&quot;).modal(&#39;hide&#39;);
                    }, 1000);
                } else {
                    console.log(&quot;empty&quot;);
                }
            }
            
        },
        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() {

    $(&quot;#modal_loader&quot;).modal(&#39;show&#39;);

    SuccessMessage(&quot;abc&quot;);
}

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 = &quot;123ABC&quot;;

    //there are better ways to convert to json like Newtonsoft
    return $&quot;{{ \&quot;d\&quot;:\&quot;{msg}\&quot; }}&quot;;
}

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.

huangapple
  • 本文由 发表于 2023年7月20日 21:39:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定