【问题标题】:Value overwriten after postback?回发后值被覆盖?
【发布时间】:2014-05-14 12:35:51
【问题描述】:

我有一个reporting.aspx 页面。如果页面加载时没有参数,它会显示项目列表,单击其中一个项目将加载带有 projectId 参数的同一页面并显示该项目的实际报告。

此报告有两个文本框:FromDateText 和 ToDateText。这两个框的文本应该是今天的日期,或者应该是 URL 的另外两个参数的值。我的问题是,它会在按钮调用 Response.Redirect 之前用今天的日期重新初始化两个框的文本。

一点代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        bool projectIdAvailable = this.Request.QueryString["ProjectID"] != null;
        if (!projectIdAvailable)
        {
            // Load list with buttons
            // Each button loads the page again with a ProjectId as parameter
            foreach (var project in ListOfAllProjects)
            {
                var button = new Button
                    {
                        Text = project.Name,
                        PostBackUrl = string.Format("Reporting.aspx?ProjectID={0}", project.Id)
                    };
                MyPanel.Controls.Add(button);
            }
        }
        else
        {
            // Can't use if (!IsPostBack) as it will always be a postback at this place.
            LoadReporting();
        }
    }

    private void LoadReporting()
    {
        // If we have date as parameter use it, else take today
        if (this.Request.QueryString["fromDate"] != null)
        {
            this.FromDateText.Text = this.Request.QueryString["fromDate"];
        }
        else
        {
            this.FromDateText.Text = DateTime.Now.ToShortDateString();
        }

        if (this.Request.QueryString["toDate"] != null)
        {
            this.ToDateText.Text = this.Request.QueryString["toDate"];
        }
        else
        {
            this.ToDateText.Text = DateTime.Now.ToShortDateString();
        }

        // Reporting generates a table here...
    }

    // Refresh the page with date parameters
    protected void RefreshButtonClick(object sender, EventArgs e)
    {
        int projectId = int.Parse(this.Request.QueryString["ProjectID"]);
        this.Response.Redirect(
            string.Format(
                "Reporting.aspx?ProjectID={0}&fromDate={1}&toDate={2}",
                projectId,
                this.FromDateText.Text,
                this.ToDateText.Text));
    }

我能想出的最佳解决方案是不在文本框中写入当前日期,这样它就不会覆盖自身,或者我可以使用一些 JavaScript,这样我就不会收到回发。

似乎都不是一个很好的方法。这样做的好方法是什么?

【问题讨论】:

  • @NicholasV.:当页面使用参数重新加载自身,然后才加载报告,如果我只在 if(!IsPostBack) 中调用 LoadReporting() 部分,则永远不会加载它。

标签: c# asp.net


【解决方案1】:

您应该只加载数据 if(!IsPostBack) 而不是每次回发。

protected void Page_Load(object sender, EventArgs e)
{
    bool projectIdAvailable = this.Request.QueryString["ProjectID"] != null;
    if (!projectIdAvailable)
    {
        // Load list with buttons
        // PostBackUrl = "Reporting.aspx?ProjectID=0"
    }
    else
    {
        if(!IsPostBack)
            LoadReporting();
    }
}

内容默认持久化在ViewState,无需每次都重新加载。

【讨论】:

  • 但是,如果我加载报告,它将始终是回发,因为您必须单击 if(!projectIdAvailable) 部分中生成的按钮之一才能到达 LoadReporting()。
  • 为什么不使用普通的 ASP.NET 按钮来回发,为什么使用Response.Redirect 来加载相同的页面?
  • 我在运行时生成按钮,因为项目的数量是可变的(我将添加更多代码)。除了 Response.Redirect 我还应该使用什么?我只想要一个根据参数做出不同反应的页面。我应该只使用两页吗?
  • 我还没有时间深入挖掘。但是为什么不使用像Repeater 这样的Web 数据绑定控件来动态创建控件呢?我什至会将它们包装在 UserControl 中,这样可以降低复杂性并提高可重用性和可维护性。您无需重新加载页面,只需将 DataSource 分配给控件(如 Repeater),然后调用 DataBind()
  • 我通常不使用 ASP.NET,所以简单的原因是:我不知道最好的方法是什么,所以我只是手动完成所有操作。但是中继器将如何改变任何事情呢?编辑:谢谢,我去看看。
【解决方案2】:

尝试在 !ispostback.. 中加载,否则每次回发时都会重置该值..

protected void Page_Load(object sender, EventArgs e)
{
    bool projectIdAvailable = this.Request.QueryString["ProjectID"] != null;
    if (!projectIdAvailable)
    {
        // Load list with buttons
        // PostBackUrl = "Reporting.aspx?ProjectID=0"
    }
    else
    {
        if(!IsPostBack)
        {
            LoadReporting();
        }
    }
}

【讨论】:

  • 但是,如果我加载报告,它将始终是回发,因为您必须单击 if(!projectIdAvailable) 部分中生成的按钮之一才能到达 LoadReporting()。
猜你喜欢
  • 2013-10-03
  • 2012-08-23
  • 2016-12-27
  • 2013-09-02
  • 2014-08-27
  • 2013-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多