Reputation: 447
I have a gridview on my web form. I have taken a hyperlink control inside the template field of gridview. I want that this hyperlink should be visible to only site admin. I have done this through Gridview_RowDataBound property. But instead of doing this I want to hide this hyperlink within page load.
This is what I have done so far.
aspx page-
<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-
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.
Upvotes: 0
Views: 3267
Reputation: 62488
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.
Upvotes: 1
Reputation: 56
Try this after after DataBind to GridView...
GridView1.Columns[3].Visible = false;
Upvotes: 0