Execute JavaScript function in code behind multiple times using for loop in ASP.Net

Last Reply on May 11, 2018 02:18 AM By mukesh1

Posted on May 10, 2018 10:15 PM

 

DataSet ds = sql.getds("select title from admin_notification ");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
    ScriptManager.RegisterStartupScript(this, typeof(Page), "Alert", "<script>showpop('" + ds.Tables[0].Rows[i]["title"].ToString() + "','" + "title" + "')</script>", false);
}

 

     <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>

     <script type="text/javascript">

       function showpop(msg, title) {
           debugger;
           // alert("ok");
           toastr.options = {
               "closeButton": false,
               "debug": false,
               "newestOnTop": false,
               "progressBar": true,
               "positionClass": "toast-bottom-left",
               "preventDuplicates": true,
               "onclick": null,
               "showDuration": "300",
               "hideDuration": "1000",
               "timeOut": "120000",
               "extendedTimeOut": "1000",
               "showEasing": "swing",
               "hideEasing": "linear",
               "showMethod": "fadeIn",
               "hideMethod": "fadeOut"
           }
           // toastr['success'](msg, title);
           var d = Date();

           toastr.success(msg, title);
           return false;
       }
    </script>

why toast msg not working on master page????on other pages only first msg showing in toast msg,not showing all msg.how to show all msg???

Posted on May 11, 2018 01:59 AM

Hi mukesh1,

I have checked its working on master page. You need to place the script inside the body tag before the ContentPlaceHolder. So that you can use the showpop method in each child page.

To show all msg refer the below code.

As per the documentation, each startup script is uniquely identified by its key and its type. Scripts with the same key and type are considered as duplicates. Add unique values to each key like this.

C#

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 3; i++)
    {
        ScriptManager.RegisterStartupScript(this, typeof(Page), "Alert" + i, "<script>showpop('" + "Description " + (i + 1) + "','" + "title " + (i + 1) + "')</script>", false);
    }
}

 


Posted on May 11, 2018 02:18 AM Modified on on May 11, 2018 02:36 AM

ok, thanks sir.

C#

DataSet ds = sql.getds("select title from admin_notification ");
Label1.Text = ds.Tables[0].Rows.Count.ToString();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
    ScriptManager.RegisterStartupScript(this, typeof(Page), "Alert" + i, "<script>showpop('" + ds.Tables[0].Rows[i]["title"].ToString()  + "','" + "title"  +"')</script>", false);
}

Script

   <script type="text/javascript">         
       function showpop(msg, title) {
           toastr.options = {
               "closeButton": false,
               "debug": false,
               "newestOnTop": false,
               "progressBar": true,
               "positionClass": "toast-bottom-left",
               "preventDuplicates": true,
               "onclick": null,
               "showDuration": "300",
               "hideDuration": "1000",
               "timeOut": "120000",
               "extendedTimeOut": "1000",
               "showEasing": "swing",
               "hideEasing": "linear",
               "showMethod": "fadeIn",
               "hideMethod": "fadeOut"
           }
           // toastr['success'](msg, title);
           var d = Date();
           var count = document.getElementById("Label1").value;
          
           toastr.success(msg, title);
           return false; 
       }
    </script>