Nick
Nick

Reputation: 189

ASP.Net Update Panel not reading Global Variables in code behind

I am trying to make a bootstrap progress bar update its percent on my site. I know ajax is the proper way to do this, but I have never used ajax and was trying to make the progress bar update without it, which I got working mostly.

this is my update panel code

<asp:UpdatePanel ID="UpdatePanel1" runat="server" RenderMode="Block" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel ID="testthis" runat="server">
            <asp:Label CssClass="Label" ID="holdstuff" Text="" Font-Size="Large" Visible="false" runat="server" />
            <div class="progress-bar progress-bar-striped active" id="testSpace" visible="false" role="progressbar"
                aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="" runat="server">
                0%
            </div>
        </asp:Panel>
        <asp:Timer ID="Timer1" runat="server" OnTick="tmrLiveTime_Tick" Interval="10000"></asp:Timer>
    </ContentTemplate>
</asp:UpdatePanel>

this is my tmrLiveTime_Tick method the update panel is calling when I had it working at its most basic level, by just adding 10% to the progress every panel update

protected void tmrLiveTime_Tick(object sender, EventArgs e)
{
    HtmlGenericControl testSpace;
    testSpace = (HtmlGenericControl)testthis.FindControl("testSpace");  //find the progress bar div
    string numpercent = Regex.Match(testSpace.InnerText, @"\d+").Value;  //grabs the percent I am showing as text on percent bar
    Int32.TryParse(numpercent, out testWidth); // turning that percent into an int
    testSpace.Style.Remove("width");  //remove old progressbar fill amount
    testWidth += 10; //adding 10 to the number I got from progress bar last value
    testSpace.Style.Add("width", holdstuff.Text + "%"); //updating progress bar fill amount
    testSpace.InnerText = Regex.Replace(testSpace.InnerText, @"\d+", holdstuff.Text);  //updating text in progress bar
    uppLiveTime.Update(); //updating panel that holds progress bar
}

I want to get the progress bar working based on a global variable that is being set outside of this panel update method. It is the percent of webcalls I have completed. So lets say I've made 8 webcalls so far and I know I'll be making 10. My global variable will hold "80"

the global variable seems to update fine as I run the program, but inside the tmrLiveTime_Tick method it doesnt read the global variable as holding a value.

I've done as far as using a non visible label inside my update panel and changed its text value to the value to what the global variable would be holding. That is what the "holdstuff" label in the update panel is. And after each web call, my breakpoints say the text of that label is updating fine, but the timer method does not read that value either.. I know that is a very bad practice way to do it, but right now I just want this working.

My timer method looks like this when trying to read from the label (its the same code when using the global variable, but replace holdstuff.Text with a Global variable name)

protected void tmrLiveTime_Tick(object sender, EventArgs e)
{
    HtmlGenericControl testSpace;
    testSpace = (HtmlGenericControl)testthis.FindControl("testSpace");
    if (holdstuff.Text != "")
    {
        testSpace.Visible = true;
        testSpace.Style.Remove("width");
        testSpace.Style.Add("width", holdstuff.Text + "%");
        testSpace.InnerText = Regex.Replace(testSpace.InnerText, @"\d+", holdstuff.Text);
    }
    else
        testSpace.Visible = false;
    uppLiveTime.Update();
}

Thank you

Upvotes: 0

Views: 518

Answers (1)

VDWWD
VDWWD

Reputation: 35554

You are talking about a global variable, but it is not very clear what you mean. I think you are using holdstuff to store your percentage value. However you never seem to be updating it in tmrLiveTime_Tick.

Take a look at the following code. It updates the holdStuff value on every Tick.

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        //set the initial value of holdStuff (inside an IsPostBack check)
        holdstuff.Text = "0";
    }
}

protected void tmrLiveTime_Tick(object sender, EventArgs e)
{
    //convert the current value of holdStuff to an int
    int currentValue = Convert.ToInt32(holdstuff.Text) + 10;

    //do your thing

    //set the new value
    holdstuff.Text = currentValue.ToString();
}

The aspx

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>

        <asp:Label CssClass="Label" ID="holdstuff" runat="server" />

        <asp:Timer ID="Timer1" runat="server" OnTick="tmrLiveTime_Tick" Interval="2000"></asp:Timer>

    </ContentTemplate>
</asp:UpdatePanel>

Upvotes: 0

Related Questions