2012-08-01 73 views
0

我用下面的代码隐藏在页面加载模板字段的ImageButton,但它不工作,提前 感谢:图像按钮不会隐藏在页面加载

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load 
      Dim ImageButton1 As ImageButton = DirectCast(GridView1.FindControl("ImageButton1"), ImageButton) 
      If User.Identity.Name.Substring(InStr(User.Identity.Name, "\")).ToUpper = "User1" Then 
       ImageButton1.Visible = False 
      End If 
     End Sub 

回答

0

假设你有binding grid before and it has rows在gridview的某行中找到ImageButton,而不是在gridview中查找。 if条件似乎永远不会变成真,因为您正在比较ToUpper后面的字符串和非大写字符串,Change User1 to USER1正如您使用的是ToUpper。通过循环

变化

Dim ImageButton1 As ImageButton = DirectCast(GridView1.FindControl("ImageButton1"), ImageButton) 
If User.Identity.Name.Substring(InStr(User.Identity.Name, "\")).ToUpper = "User1" Then 
      ImageButton1.Visible = False 
End If 

  Dim ImageButton1 As ImageButton = DirectCast(GridView1.Rows(0).FindControl("ImageButton1"), ImageButton) 
If User.Identity.Name.Substring(InStr(User.Identity.Name, "\")).ToUpper = "USER1" Then 
     ImageButton1.Visible = False 
End If 

遍历整个网格

For Each row As GridViewRow In GridView1.Rows 
Dim ImageButton1 As ImageButton = DirectCast(row.FindControl("ImageButton1"), ImageButton) 
    If User.Identity.Name.Substring(InStr(User.Identity.Name, "\")).ToUpper = "USER1" Then 
      ImageButton1.Visible = False 
    End If 
Next 
+0

阿迪尔感谢您的快速反应,我想乌尔Ç它的工作,但只为第一行(索引= 0)。你能否在页面加载中包含绑定网格的代码? – user851971 2012-08-01 19:14:31

+0

你需要使用循环遍历整个网格,检查我更新的答案。 – Adil 2012-08-01 22:37:45

+0

chEERS,Adil ...像伏都教一样工作! – user851971 2012-08-02 13:53:13