2010-12-11 69 views
5

我成功創建了用於顯示錯誤消息的用戶控件。現在一切正常,但顯示消息時可以訪問後臺控件。如何禁用頁面控件或頁面點擊或選擇任何控件。當消息面板關閉時,它應該啓用頁面控件。如何在選擇usercontrol時禁用ASP.NET頁面中的控件?

我找到了答案的朋友。

void DisableControls(Control parent, bool status) 
    { 
    foreach (Control c in parent.Controls) 
      { 
       if (c is DropDownList) 
       { 
        ((DropDownList)(c)).Enabled = status; 
       } 
       if (c is Button) 
       { 
        ((Button)(c)).Enabled = status; 
       } 
       if (c is TextBox) 
       { 
        ((TextBox)c).Enabled = status; 
       } 

       if (c is RadioButton) 
       { 
        ((RadioButton)c).Enabled = status; 
       } 
       if (c is ImageButton) 
       { 
        ((ImageButton)c).Enabled = status; 
       } 
       if (c is CheckBox) 
       { 
        ((CheckBox)c).Enabled = status; 
       } 
       if (c is DropDownList) 
       { 
        ((DropDownList)c).Enabled = status; 
       } 
       if (c is HyperLink) 
       { 
        ((HyperLink)c).Enabled = status; 
       } 
       if (c is GridView) 
       { 
        ((GridView)c).Enabled = status; 
       } 
       if (c is Table) 
       { 
        ((Table)c).Enabled = status; 
       } 
       if (c is Menu) 
       { 
        ((Menu)c).Enabled = status; 
       } 
       if (c is TreeView) 
       { 
         ((TreeView)c).Enabled = status; 
        } 
} 
     } 
+0

當用戶控件被激活時,我得到了div中我添加了controls.but的頁面中的控件從我調用的地方也是活動的。我想禁用該頁面中的控件。 – 2010-12-11 14:36:45

+0

我想你會發現你可以通過簡單地使用下面的代碼來大大簡化你的代碼*:foreach(parent.Controls中的Control c){c.Enabled = false; }因爲Enabled是Control的一個屬性。 – Crisfole 2010-12-20 14:43:42

+0

感謝您的回覆。我已經嘗試過,但不能成功完成。它會拋出一個錯誤。 – 2010-12-20 15:19:25

回答

2

我明白了,你想讓它像一個模態對話框。它可以通過簡單的html + javascript完成。 您必須創建一個覆蓋整個頁面的透明div覆蓋圖,以便用戶不用點擊控件就可以點擊div。 Z-index指示其餘控件的位置。

<!-- Div Overlay --> 
<div id="div-overlay" style="position: absolute; height: 100%; width: 100%; z-index: 200; display: none; opacity: 0.0"></div> 

<!-- Scripts to show/hide overlay --> 
<script type="text/javascript"> 
function showOverlay() { 
    var e = document.getElementById('div-overlay'); 
    e.style.display = 'block'; 
} 

function hideOverlay() { 
    var e = document.getElementById('div-overlay'); 
    e.style.display = 'none'; 
} 
</script> 

希望它有幫助。

1

您可以簡單地用一個DIV與CSS的幫助下,你可以能夠顯示該div就像一個模式彈出或簡單地使用jQuery的模態彈出http://jqueryui.com/demos/dialog/或asp.net ajaxcontrol工具http://www.asp.net/ajax/ajaxcontroltoolkit/samples/modalpopup/modalpopup.aspx

+0

不,我不想在這個使用Ajax,因爲我有一些像頁面閃爍等問題。 – 2010-12-11 14:34:50

+0

如果可以使用JQuery,或者只是簡單地使用CSS來顯示像模態彈出窗口,然後所有其他控件將不可訪問 – 2010-12-11 14:38:35

+0

你能給我一個例子 – 2010-12-11 15:05:45

0

是你試圖創建一個模態對話框?如果是的話,你可以使用asp.net ajax中的ModalPopupExtender控件。檢查此鏈接:

http://msdn.microsoft.com/en-us/magazine/cc164247.aspx

+0

沒有我不在其中使用ajax – 2010-12-11 14:33:48

+0

您是否嘗試過使用此控件?我不認爲你會得到任何問題,如頁面閃爍。 – sid 2010-12-11 19:18:13

+0

當頁面重新加載或刷新模型彈出窗口時,它會持續幾毫秒 – 2010-12-12 05:37:57

相關問題