简体   繁体   English

如何通过页面内部加载背后的代码在gridview中隐藏超链接控件?

[英]How to hide a hyperlink control inside gridview through code behind inside page load?

I have a gridview on my web form. 我的Web表单上有一个gridview。 I have taken a hyperlink control inside the template field of gridview. 我已经在gridview的模板字段内进行了超链接控件。 I want that this hyperlink should be visible to only site admin. 我希望此超链接仅对站点管理员可见。 I have done this through Gridview_RowDataBound property. 我已经通过Gridview_RowDataBound属性完成了此操作。 But instead of doing this I want to hide this hyperlink within page load. 但是,我不想在页面加载中隐藏此超链接,而不是这样做。

This is what I have done so far. 到目前为止,这是我所做的。

aspx page- aspx页面

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" onrowdatabound="GridView1_RowDataBound1"
BorderStyle="None" EnableModelValidation="True" ShowHeader="False" Width="1000px" GridLines="None">
<Columns>
<asp:TemplateField>
                <ItemTemplate>
<asp:HyperLink ID="HyperLink2" runat="server" Font-Bold="True" Font-Size="Small" 
                        ForeColor="#FF3300" CommandName="EDT" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
                        NavigateUrl='<%# Eval("id","test1.aspx?id={0}") %>'>HyLink</asp:HyperLink>
</ItemTemplate>
            </asp:TemplateField>

</Columns>
</asp:GridView>

cs page- CS页面

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (Convert.ToString(Session["logname"]) != "Admin")
            {
                HyperLink Hlnk = e.Row.FindControl("HyperLink2") as HyperLink;
                Hlnk.Visible = false;
            }
        }
    }

How can I do this within page load? 如何在页面加载内执行此操作? Please guide. 请指导。

try this: 尝试这个:

foreach(GridViewRow row in GridView1.Rows) {
    if(row.RowType == DataControlRowType.DataRow) {
        HyperLink myHyperLink = row.FindControl("HyperLink2") as HyperLink;
        myHyperLink.Visible = false;
    }
}

or: 要么:

for (int i = 0; i < GridView1.Rows.Count; i++)
 {
  HyperLink myHyperLink = (HyperLink)gvExcParts.Rows[i].FindControl("HyperLink2");
  myHyperLink.Visible = false;
 }

but make sure bind data to gride view then find controls inside it, otherwise this will not work. 但请确保将数据绑定到网格视图,然后在其中查找控件,否则将无法使用。

在将DataBind转换为GridView之后再尝试...

GridView1.Columns[3].Visible = false;

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

相关问题 如何根据数据行计数从后面的代码向&#39;Repeater control&#39;中的&#39;hyperlink&#39;添加css类 - How to add css class to 'hyperlink' inside 'Repeater control' from code behind based on data row counting 如何通过使用后面的代码在Gridview的itemtemplate内的控件上添加只读属性? - How can I add readonly attribute at a control inside itemtemplate of Gridview by using code behind? 在源的可见属性中隐藏gridview超链接,而不是后面的代码 - hide gridview hyperlink in visible property of source rather then code behind 禁用/隐藏GridView特定行内的控件 - Disable/ Hide a control inside a specific row of GridView 从背后的代码在Web用户控件上的Listview ItemTemplate中填充Gridview - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind 从代码隐藏访问另一个GridView内部的GridView - Access GridView inside another GridView from code-behind 如何在 c# 代码后面将值绑定到 gridview 中的超链接 - how to bind value to hyperlink in gridview in c# code behind 将代码隐藏方法绑定到gridview中的超链接 - Bind a code-behind method to hyperlink in gridview 如何在页面加载时在devexpress gridview中隐藏列? - How to hide a column in devexpress gridview on page load? GridView内的DomainUpDown控件 - DomainUpDown control inside the GridView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM