Hiding non-databound hyperlinks in a DataGrid

B

BobRoyAce

I am brand new to ASP.NET and am now required to take over maintenance of a
..NET/C# web application. On one of the pages I'm working on there is a
DataGrid which has multiple columns. One of the columns is not databound and
instead has up to three hyperlinks in it (Edit, Award, and Cancel). A couple
of the hyperlinks should not be shown in certain cases. Now, mind you, I
don't know if it's being done in the way that's best to do it. It looks like
there is a "panel" being created to contain the hyperlinks that may not be
visible. I don't know why though (i.e. why not just set hyperlink's Visible
property and be done with it without a panel?). That being said, I want to
change the page so that one of the hyperlinks does not show (i.e. Visible =
False) based on a certain value for a session cookie. It's the Award link in
the code shown below from the .aspx file in question.

--- CODE BEGINS ---

<asp:TemplateColumn HeaderText="Actions">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:HyperLink id="hlEdit" runat="server" NavigateUrl='<%#
"Edit.aspx?id=" + DataBinder.Eval(Container, "DataItem.PersonID") %>'
CssClass="link-text"> <!--onMouseOver='<%# "javascript:toolTip(\"" +
DataBinder.Eval(Container, "DataItem.PersonToolTip") + "\");" %>'
onMouseOut="javascript:toolTip();">-->
Edit
</asp:HyperLink>
<asp:panel ID="Panel1" Runat="server" Visible='<%#
Convert.ToBoolean(Convert.ToInt32(Request.Cookies["core"]["sl"]) <= 9)%>'>
<asp:HyperLink id=hlBids runat="server" Visible='<%#
Convert.ToBoolean(Convert.ToInt32(Request.Cookies["core"]["sl"]) <= 9)%>'
NavigateUrl='<%# "Award.aspx?id=" + DataBinder.Eval(Container,
"DataItem.PersonID") %>' CssClass="link-text">
Award
</asp:HyperLink>
</asp:panel>
<asp:panel ID="panAction" Runat="server" Visible='<%#
Convert.ToBoolean(DataBinder.Eval(Container,
"DataItem.PersonStatusCancel"))%>'>
<asp:HyperLink id="hlCancel" runat="server" NavigateUrl='<%#
"Cancel.aspx?id=" + DataBinder.Eval(Container, "DataItem.PersonID") +
"&op=cancel" %>' Visible='<%# Convert.ToBoolean(DataBinder.Eval(Container,
"DataItem.PersonStatusCancel"))%>' CssClass="link-text" >
Cancel
</asp:HyperLink>
</asp:panel>
</ItemTemplate>
</asp:TemplateColumn>

--- CODE ENDS ---

Now, the value of the cookie, when I bring up this page, is 1, which
satisfies the condition...yet, the link still shows. I guess my question is
two-part:
1) What is the best way to accomplish something like this? Do I have to use
a DataList instead or can it be done using a DataGrid?
2) If this is best way to do it, then what's wrong with code above?
 
D

dwight0

Wrong group, but anyways:
Try this in the Item Created event. Pseudocode:

private void DataGrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if ((int) Request.Cookies["core"]["sl"]) < 9)
{
Panel p=(Panel) e.Item.Cells[0].FindControl("Panel1");
if (p != null) //make sure we found the item
p.Visible=false;
}
}
 
D

dwight0

A right group would be
microsoft . public . dotnet . framework . aspnet
instead of windows forms
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top