简体   繁体   中英

Pass value to Jquery function from code behind

I am using Jquery progress bar control on asp.net form to show the percentage of completed work for inspector. Here is the client side function for progress bar.

    <script>
        $(function () {
            $("#progressbar").progressbar({
                value: 37
            });
        });
    </script>

    <form id="form1" runat="server">
       <div id="progressbar"></div>
    </form>

Is there any way to set progress bar value from code behind page? Values are read from database.

You can use registerstartupscript to inject the script from ASP.NET code behind files.

myScript = "\n<script type=\"text/javascript\" language=\"Javascript\" id=\"EventScriptBlock\">\n";
        myScript += "ShowProgressBar(35);"; //35 is dynamic value
        myScript += "\n\n </script>";
     Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", myScript, false);

JavaScript Code

function ShowProgressBar(value) 
{
    $("#progressbar").progressbar({
         value: value
     });
 }

Try below steps

  1. First set the DB value to hidden field on page load. like ( HiddenFieldID.value="37" // from DB )
  2. Use the hidden field value to set the value to progress bar in js function as below.

$(function () { $("#progressbar").progressbar({ value: $("#<%= HiddenFieldID.ClientID %>").val(); }); });

您可能要尝试:

ScriptManager.RegisterClientScriptBlock(Me.Page, Me.Page.GetType(), "progressbar", "ShowProgressBar("+ 35 +");", True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM