一段時間以來,我試圖弄清楚如何重構我的一些代碼以減少整個應用程序的冗餘。我只是學習OOP的基礎知識,可以創建簡單的類和方法,但我的知識在實際應用方面受到限制。下面的代碼位說明了我的無奈:ASP.Net C# - 從Codebehind移動代碼到類文件
#region DELETE selected users - button
protected void btnDeleteSelected_Click(object sender, EventArgs e)
{
try
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkRows");
if (cb != null && cb.Checked)
{
// get the row index values (DataKeyNames) and assign them to variable
string userName = GridView1.DataKeys[row.RowIndex].Value.ToString();
// delete selected users and their profiles
ProfileManager.DeleteProfile(userName);
Membership.DeleteUser(userName);
Msg.Text = "User(s) were sucessfully <b>DELETED</b>!";
Msg.Visible = true;
}
}
}
catch (Exception ex)
{
Msg.Text = "Oops! " + ex.Message;
Msg.Visible = true;
}
finally
{
// refresh gridview to reflect changes
GridView1.DataBind();
}
}
#endregion
這段代碼是我在頁面代碼隱藏的項目文件的多個頁面中使用。我如何將它移到一個類文件中。我不知道如何在類中引用像gridview這樣的對象,因爲它不像它在實際頁面上那樣存在。
請問有人能幫忙嗎?謝謝。
如果沒有看到ASP以及背後代碼中的其他示例方法,將很難推薦如何最好地重構此代碼。你可以多發一點這個項目嗎? – 2010-07-17 00:49:11