ページに複数のドロップダウンリストがあり、ユーザーが[すべて無効にする]チェックボックスを選択した場合にすべて無効にしたい。これまでのところ、私はこのコードを持っていますが、機能していません。助言がありますか?
foreach (Control c in this.Page.Controls)
{
if (c is DropDownList)
((DropDownList)(c)).Enabled = false;
}
ページに複数のドロップダウンリストがあり、ユーザーが[すべて無効にする]チェックボックスを選択した場合にすべて無効にしたい。これまでのところ、私はこのコードを持っていますが、機能していません。助言がありますか?
foreach (Control c in this.Page.Controls)
{
if (c is DropDownList)
((DropDownList)(c)).Enabled = false;
}
各コントロールには子コントロールがあるため、それらすべてにアクセスするには再帰を使用する必要があります。
protected void DisableControls(Control parent, bool State) {
foreach(Control c in parent.Controls) {
if (c is DropDownList) {
((DropDownList)(c)).Enabled = State;
}
DisableControls(c, State);
}
}
次に、次のように呼び出します。
protected void Event_Name(...) {
DisableControls(Page,false); // use whatever top-most control has all the dropdowns or just the page control
} // divs, tables etc. can be called through adding runat="server" property
これは古い投稿であることは知っていますが、これが私がこの問題を解決した方法です。「ASP.NET ページのすべてのコントロールを無効にするにはどうすればよいですか?」というタイトルのとおりです。これを実現するためにリフレクションを使用しました。Enabled プロパティを持つすべてのコントロール タイプで機能します。親コントロール (つまり、フォーム) を渡して DisableControls を呼び出すだけです。
C#:
private void DisableControls(System.Web.UI.Control control)
{
foreach (System.Web.UI.Control c in control.Controls)
{
// Get the Enabled property by reflection.
Type type = c.GetType();
PropertyInfo prop = type.GetProperty("Enabled");
// Set it to False to disable the control.
if (prop != null)
{
prop.SetValue(c, false, null);
}
// Recurse into child controls.
if (c.Controls.Count > 0)
{
this.DisableControls(c);
}
}
}
VB:
Private Sub DisableControls(control As System.Web.UI.Control)
For Each c As System.Web.UI.Control In control.Controls
' Get the Enabled property by reflection.
Dim type As Type = c.GetType
Dim prop As PropertyInfo = type.GetProperty("Enabled")
' Set it to False to disable the control.
If Not prop Is Nothing Then
prop.SetValue(c, False, Nothing)
End If
' Recurse into child controls.
If c.Controls.Count > 0 Then
Me.DisableControls(c)
End If
Next
End Sub
無効にしたいすべてのコントロールをパネルに配置してから、パネルを有効/無効にするだけで簡単にできます。
無効にするページの部分にパネルを配置します。
< asp:Panel ID="pnlPage" runat="server" >
...
< /asp:Panel >
Page_Load の内部:
If Not Me.Page.IsPostBack Then
Me.pnlPage.Enabled = False
End If
... または同等の C# です。:o)
私はこれが好きだったASP.NetとHTMLコントロールで作業していました
public void DisableForm(ControlCollection ctrls)
{
foreach (Control ctrl in ctrls)
{
if (ctrl is TextBox)
((TextBox)ctrl).Enabled = false;
if (ctrl is Button)
((Button)ctrl).Enabled = false;
else if (ctrl is DropDownList)
((DropDownList)ctrl).Enabled = false;
else if (ctrl is CheckBox)
((CheckBox)ctrl).Enabled = false;
else if (ctrl is RadioButton)
((RadioButton)ctrl).Enabled = false;
else if (ctrl is HtmlInputButton)
((HtmlInputButton)ctrl).Disabled = true;
else if (ctrl is HtmlInputText)
((HtmlInputText)ctrl).Disabled = true;
else if (ctrl is HtmlSelect)
((HtmlSelect)ctrl).Disabled = true;
else if (ctrl is HtmlInputCheckBox)
((HtmlInputCheckBox)ctrl).Disabled = true;
else if (ctrl is HtmlInputRadioButton)
((HtmlInputRadioButton)ctrl).Disabled = true;
DisableForm(ctrl.Controls);
}
}
このように呼ばれる
DisableForm(Page.Controls);
private void ControlStateSwitch(bool state)
{
foreach (var x in from Control c in Page.Controls from Control x in c.Controls select x)
if (ctrl is ASPxTextBox)
((ASPxTextBox)x).Enabled = status;
else if (x is ASPxDateEdit)
((ASPxDateEdit)x).Enabled = status;
}
私はlinqアプローチを使用しています。devExpress を使用している間は、DevExpress.Web.ASPxEditors ライブラリを含める必要があります。
これは、オプションのパラメーターも取る VB.NET バージョンで、コントロールを有効にするためにも使用できます。
Private Sub SetControls (コントロールとしての ByVal 親コントロール、ブール値としてのオプションの ByVal 有効化 = False)
For Each c As Control In parentControl.Controls
If TypeOf (c) Is CheckBox Then
CType(c, CheckBox).Enabled = enable
ElseIf TypeOf (c) Is RadioButtonList Then
CType(c, RadioButtonList).Enabled = enable
End If
SetControls(c)
Next
End Sub