2017-08-07 51 views
0

我想在可用數據的情況下使webform中的按鈕變爲可見。 我有一個列名爲NCID的數據庫,然後在一個視圖中我計算了NCID。 和我有一個btn1直到btn9隱藏。 如果NCID計數爲1,則顯示按鈕btn1, 如果NCID計數爲2,則顯示btn1和btn2。基於C#中的數據庫值打開和關閉按鈕#

如何鎖定按鈕ID以使其可見或隱藏?

我試過以下,但它不適合我。

while (sr.Read()) 
{ 
    string NCID = sr["NCID"].ToString(); 
    int nc2 = Convert.ToInt32(NCID); 
    int x = 1; 

    do 
    { 
     string btnx = "btn" + x; 
     btnx.Visible = true; 

     x++; 
    } while (x <= nc2); 
} 

con.Close(); 
+0

這是web表單,MVC,WPF? – Steve

+0

'我試過以下,但它不適合我!!'定義'不工作'。你期望會發生什麼?究竟發生了什麼? – mjwills

+0

對不起,這是一個網絡表格 –

回答

1

如果這是一個Windows窗體應用程序,你可以使用:

Controls.Find(btnx, true).First().Visible = true; 

Find control by name from Windows Forms controls

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.find(v=vs.110).aspx

如果是的WebForms,您可以使用:

FindControl(btnx).Visible = true; 

https://msdn.microsoft.com/en-us/library/486wc64h(v=vs.110).aspx

+0

我試過Controls.FindControl(btnx).Visible = true;那麼我得到一個錯誤「ControlCollection不包含FindControl的定義」 –

+0

你能否更新你的問題以顯示你如何在HTML中聲明控件? – Jared

+0

string NCID = sr [「NCID」]。ToString(); int nc2 = Convert.ToInt32(NCID); int x = 1; 做 { string btnx =「btn」+ x; Controls.FindControl(btnx).Visible = true; x ++; } while(x <= nc2); –

0

(見jareds接聽)

for (int i = 0; i < x; i++) //smaller then ammount of lines returned  
{ 
    Controls.Find(btnx + i).Visible = true; //add visibility to wichever control (found by name) you wanted to add 
}