[ASP.NET] foreach 문을 활용하여 컨트롤 제어하기
private void Page_Load(object sender, System.EventArgs e)
{
// 비활성화 되어 있는 컨트롤을 초기화 함.
SetControl(this) ;
}
/// <summary>
/// 2007/10/30 - add by bhchoi
/// 비활성화 되어있는 컨트롤을 초기화 하도록 함.
/// </summary>
/// <param name="Page">Control</param>
private void SetControl(Control Page)
{
foreach (Control ctrl in Page.Controls)
{
if (ctrl is TextBox)
{
if (((TextBox)(ctrl)).ReadOnly) // ReadOnly = True(필수입력아님)인 Textbox 값을 초기화 함.
{
((TextBox)(ctrl)).Text = "" ;
}
}
else if (ctrl is DropDownList)
{
if (((DropDownList)(ctrl)).Enabled == false) // Enabled = false(필수입력아님)인 DropDownList 값을 초기화 함.
{
((DropDownList)(ctrl)).Items.Clear() ;
}
}
else
{
if (ctrl.Controls.Count > 0)
{
SetControl(ctrl);
}
}
}
}
※ 주석에 있는 내용과 같이 비활성화 되어있는 컨트롤(TextBox, DropDownList)을 찾아 값을 초기화 해주는 방법입니다. 위의 방법을 참고해서 여러가지 활용할 수 있을거 같은 냄새가 풀풀 나네요...=.=