图像按钮不会在页面加载时隐藏

时间:2012-08-01 16:24:58

标签: asp.net imagebutton directcast

我使用下面的代码在页面加载时隐藏模板字段图像按钮,但它不起作用, 提前致谢:

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

1 个答案:

答案 0 :(得分:0)

假设你有binding grid before and it has rows在某行网格中找到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