【问题标题】:ASP.Net Update Panel not reading Global Variables in code behindASP.Net 更新面板未在后面的代码中读取全局变量
【发布时间】:2018-03-01 16:48:51
【问题描述】:

我正在尝试让引导进度条在我的网站上更新其百分比。我知道 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”标签。在每次网络调用之后,我的断点说该标签的文本正在更新,但计时器方法也不会读取该值。我知道这是一种非常糟糕的做法,但现在我只想要这个在职的。

尝试从标签中读取时,我的计时器方法看起来像这样(使用全局变量时的代码相同,但将 holdtuff.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】:

您在谈论一个全局变量,但您的意思不是很清楚。我认为您正在使用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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多