简体   繁体   English

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

[英]Image Button does not hide on page load

I used code below to hide template field Imagebutton on pageload but it DOES NOT work, Thanks in advance: 我使用下面的代码在页面加载时隐藏了模板字段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

Assuming you have binding grid before and it has rows . 假设您binding grid before and it has rowsbinding grid before and it has rows Find the ImageButton in some row of grid instead of find within gridview . 在网格的某些行中找到ImageButton,而不是在gridview中找到 The if condition you have seems to never get true, as you are comparing string after ToUpper with a string that is not in upper case, Change User1 to USER1 As you are using ToUpper. if您要比较的if条件似乎永远不会成立,因为您正在将ToUpper之后的字符串与非大写的字符串进行比较,请在使用ToUpper时将Change User1 to USER1

Change 更改

 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

To

   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

Iterating whole grid by loop 循环遍历整个网格

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM