我使用下面的代码在页面加载时隐藏模板字段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
假设您有binding grid before and it has rows
在某行网格中查找ImageButton,而不是在gridview中查找。您所具有的if
条件似乎永远不会变为真,因为您正在将ToUpper之后的字符串与不使用大写字母的字符串Change User1 to USER1
进行比较。
更改
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