【发布时间】:2009-04-06 09:49:58
【问题描述】:
大约几个月以来,我正在编写 ASP C#。我总是在事件中编写很多代码,在加载事件中我检查查询字符串是否有有效数据。这是我在一个项目中的一些示例代码:
protected void Page_Load(object sender, EventArgs e)
{
if (Controller.Manual == null)
{
Response.Redirect("login.aspx");
}
lblLocation.Text = "<a href='viewdocument.aspx'>" + Controller.Manual.Title + "</a>";
if (Request.QueryString["gchap"] != null)
{
if (Controller.IsNumeric(Request.QueryString["gchap"].ToString()))
{
genchap = Convert.ToInt32(Request.QueryString["gchap"]);
FillGeneralList();
SetChapterTitle();
}
}
if (Request.QueryString["qchap"] != null)
{
if (Controller.IsNumeric(Request.QueryString["qchap"].ToString()))
{
qualchap = Convert.ToInt32(Request.QueryString["qchap"]);
FillQualityList();
SetChapterTitle();
}
}
// Check document Id is set (did)
if (Request.QueryString["did"] != null)
{
if (Controller.IsNumeric(Request.QueryString["did"].ToString()))
{
docId = Convert.ToInt32(Request.QueryString["did"]);
DetermineView();
}
}
}
我知道必须有一种更简洁的方式来完成此任务。这只是加载事件。在其他事件上,例如 click 和 onchange 事件,我有类似的代码。我认为这是意大利面条代码,并且安排得不好。那么你能告诉我如何安排我的代码吗?
编辑:
我想知道的是,有没有更简洁的方法来填充列表框?我在哪里检查查询字符串值是否有有效数据?我在哪里检查(输入/查询字符串)数据是否是数字?您应该将验证查询字符串的代码放在哪里?也在加载事件中?
【问题讨论】:
标签: c# user-interface refactoring