ASP.Net Update Panel没有在后面的代码中读取全局变量

问题描述 投票:0回答:1

我正在尝试使引导进度条更新其在我的网站上的百分比。我知道ajax是执行此操作的正确方法,但我从未使用过ajax并且尝试在没有它的情况下更新进度条,我主要使用它。

这是我的更新面板代码

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

这是我的tmrLiveTime_Tick方法,当我在最基本的级别工作时,更新面板正在调用,只需在每个面板更新中添加10%的进度

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
}

我想基于在此面板更新方法之外设置的全局变量来使进度条工作。这是我完成的网络摄像头的百分比。所以说我到目前为止已经制作了8个网络摄像头,我知道我将制作10个。我的全局变量将保持“80”

当我运行程序时,全局变量似乎更新正常,但在tmrLiveTime_Tick方法中,它不会将全局变量读取为保存值。

我已经完成了在更新面板中使用不可见标签并将其文本值更改为全局变量将保持的值。这就是更新面板中的“holdstuff”标签。在每次网络通话后,我的断点都说该标签的文字正在更新,但是计时器方法也没有读取该值。我知道这是一种非常糟糕的练习方式,但是现在我只想要这个工作。

尝试从标签读取时,我的计时器方法看起来像这样(使用全局变量时它的代码相同,但用全局变量名替换holdstuff.Text)

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();
}

谢谢

c# html asp.net progress-bar updatepanel
1个回答
0
投票

你在谈论一个全局变量,但你的意思并不是很清楚。我认为您使用holdstuff存储您的百分比值。但是你似乎永远不会在tmrLiveTime_Tick中更新它。

看看下面的代码。它会更新每个Tick上的holdStuff值。

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();
}

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>
© www.soinside.com 2019 - 2024. All rights reserved.