【发布时间】: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() 部分,则永远不会加载它。