2017-08-07 66 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 
}