2

How do you pass a parameter from C# into the value part of instead of hard coding it with different values.

<div class="property-container">
      <input class="progress-value" type="hidden" value="17" />           
        <div class="property-title">Test</div> 
        <div class="property-progress"></div>
    </div>    

    <div class="property-container">
        <input class="progress-value" type="hidden" value="32" />
        <div class="property-title">Test 2</div> 
        <div class="property-progress"></div>
    </div>    

    <div class="property-container">
        <input class="progress-value" type="hidden" value="24" />
        <div class="property-title">Test 3</div> 
        <div class="property-progress"></div>
    </div>    

        <script type="text/javascript" >/
            $(function () {
                // loop through each 'container'
                $(".property-container").each(function () {


                    // get the value that was rendered from the model
                    var progress = parseInt($(this).children(".progress-value").val());

                    // create the progress bar with the value
                    $(this).children(".property-progress").progressbar({ value: progress });

                });
            });

        </script>
6
  • 1
    Where do you want the value? Can you not just call a method like <%=GetValue()%> Commented Sep 24, 2013 at 19:22
  • <input class="progress-value" type="hidden" value="17" id="Test" /> In c# I tried, Test.Value = "20"; but its not working Commented Sep 24, 2013 at 19:25
  • Are you using webforms? mvc? Commented Sep 24, 2013 at 19:25
  • Is this a real time progress bar? Commented Sep 24, 2013 at 19:26
  • 1
    UpdatePanel might be overkill here. Commented Sep 24, 2013 at 19:31

4 Answers 4

2

From your comments

<input class="progress-value" type="hidden" value="17" id="Test" /> 
In c# I tried, Test.Value = "20";

You should have a runat="server" if you want to access the hidden field in your codebehind.

<input class="progress-value" runat="server" type="hidden" value="17" id="Test" /> 

 //& then in C# set it like this..

 Test.Value = "20";

In jQuery, get the value of the hidden field

$('.progress-value').val()
Sign up to request clarification or add additional context in comments.

4 Comments

Do you know what is a proper way to refresh this progress bar every few seconds. Like I said I am getting dynamic data from a C# string.
you can use jQuery Ajax for it, UpdatePanel is one way. Depending on your requirement & complexity.. but thats a whole another topic.
Instead of solving the problem you went to a different ( less better) approach.
@RoyiNamir - thanks for the comment.. please look at what OP has tried in the comments section. I explained him where he went wrong.
2

You can use this :

 $(this).children(".property-progress").progressbar({ value: '<%= MyValue() %>'});

And in the c# create a property:

Public string MyValue
{
  get;set;
}

Nb

If you intend to update the progressbar according to server activity - i'd suggest you to google signalR.

5 Comments

public string MyValue { get; set; } protected void Page_Load(object sender, EventArgs e) { MyValue = "25"; }
@Apollo You set a value in the server "25" , and then you need to see the progress bar at "25%"...right ?
try value: <%= MyValue() %> ( no ')
I get this. The name 'MyValue' does not exist in the current context
@Apollo it is the aspx of the page...right ? so it should know it.
0

You just have to remember where the variables are set, and where the code is run. C# runs on the server, so you can't set javascript properties directly, rather, you need to set up your HTML template so that when it is interpolated by the server the correct values are sent to the client.

In other words, you won't be able to set the progress bar width on the server, b/c the server will only be able to set the initial value of the progress bar. It will be up to the client to increment the value. Otherwise, you'd need to constantly be posting the page back to the server, which isn't what you want at all.

The easiest way to get the progress data from the server to the client is by setting up a web service that returns JSON, and calling it using jquery.

Here are some resources on how to do that:

You should be able to decorate a method on your asp.net class with [WebMethod] to make it callable via jQuery.

Comments

0

It really depends on what technology you're using to output code. If you're using Razor, it would be:

<input value="@ObjectName.Value">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.