大多使用jquery + html方式來做
同事問到是否能將所有control變readonly
花了幾分鐘寫了以下的function希望對大家有幫助
變ReadOnly
void ReadOnlyControl(Control control)
{
foreach (var item in control.Controls)
{
if (item is System.Web.UI.WebControls.TextBox)
{
TextBox ctrl = (TextBox)item;
ctrl.ReadOnly = true;
}
else if (item is System.Web.UI.WebControls.DropDownList)
{
DropDownList ctrl = (DropDownList)item;
ctrl.Enabled = false;
}
else if (item is System.Web.UI.WebControls.CheckBox)
{
CheckBox ctrl = (CheckBox)item;
ctrl.Enabled = false;
}
}
}
變回來
void WriteControl(Control control)
{
foreach (var item in control.Controls)
{
if (item is System.Web.UI.WebControls.TextBox)
{
TextBox ctrl = (TextBox)item;
ctrl.ReadOnly = false;
}
else if (item is System.Web.UI.WebControls.DropDownList)
{
DropDownList ctrl = (DropDownList)item;
ctrl.Enabled = true;
}
else if (item is System.Web.UI.WebControls.CheckBox)
{
CheckBox ctrl = (CheckBox)item;
ctrl.Enabled = true;
}
}
}
使用方式
ReadOnlyControl(form1); WriteControl(form1);