aspcon updatepanel在usercontrol里面重新加载整个控件

时间:2015-03-10 19:46:36

标签: asp.net updatepanel

我想创建一个代表进度条的用户控件。

ASCX:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ProgressBar.ascx.cs" Inherits="A2Controls.Controls.ProgressBar" %>

<div class="progress">
    <div class="progress-bar" role="progressbar" aria-valuenow="<%# this.Value %>" aria-valuemin="0" aria-valuemax="100" style="width: <%# this.Value %>%">
        <span class="sr-only"><%# this.Value %> Complete</span>
    </div>
</div>

代码背后:

public partial class ProgressBar : System.Web.UI.UserControl
{
    public int Value { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

ASPX:

<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:Timer runat="server" ID="clock" OnTick="clock_Tick" Interval="1000" Enabled="true"></asp:Timer>
        <uc1:ProgressBar runat="server" ID="ProgressBar" />
    </ContentTemplate>
    <Triggers  >
        <asp:AsyncPostBackTrigger ControlID="clock" EventName="Tick" />
    </Triggers>
</asp:UpdatePanel>

代码背后:

protected void clock_Tick(object sender, EventArgs e)
{
    this.ProgressBar.Value++;
}

问题是每次调用tick函数时,updatepanel都会刷新并重新加载整个usercontrol(调用我的意思是构造函数)!我试图将updatepanel逻辑放在usercontrol中,但没有区别。

如何阻止usercontrol再次重新实例化?

1 个答案:

答案 0 :(得分:1)

您必须将Value属性更改为:

public int Value
{
    get
    {
        if (ViewState["Value"] != null)
            return (int)ViewState["Value"];
        else
            return 0;
    }
    set
    {
        ViewState["Value"] = value;
    }
}

通过这种方式,您可以在回发中保留属性的值。

HTTP是无状态协议。不幸的是,您必须使用某种技巧来保存ViewState之类的请求中的数据。你最好看看herehere,以便深刻理解这里没有错。

相关问题