Datagrid Hyperlink Doubt

A

Arun

Hi ,

I have doubt regarding disabling Hyerlink in Datagrid. For some cases I
don't want to show Hyperlink
and some cases I want to show the Hyperlink Column. I don't want to hide the
entire Column.

-thanks
Arun
 
B

Brock Allen

You will have to then hide the indvidual controls in the cell that you don't
want visible. A good place to do that is in the ItemDataBound event of the
Grid, or you can build your own TemplateColumn.
 
S

Steven Cheng[MSFT]

Thanks for Brock's good suggestions.

Hi Arun,

IMO, if your want to individually hidden some certain controls in a
columns, I think at first we should choose TemplateColumn since its the
most flexible one. Then, we should explicitly assign an "ID" for those
controls we want to reference later. After that, we can access those
controls in DataGrid's ItemDataBound/ItemCreated event or in any other
control (on the same page)'s post back event and modify those controls'
propety.

For example, suppose we have the following datagrid template:

<form id="Form1" method="post" runat="server">
<asp:DataGrid id="dgLink" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="title" HeaderText="Title"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Link">
<ItemTemplate>
<asp:HyperLink id="hlVisit" runat="server" NavigateUrl='<%#
((System.Data.DataRowView)Container.DataItem)[1] %>'>Visit</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="btnSubmit" runat="server" Text="Submit"></asp:Button>
</form>


Then, we can hidden the hyperlink controls in some certain rows throw
ItemDataBound event or in the button's postback event like below:

======= using itemdatabound event=========
private void dgLink_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
if(e.Item.ItemIndex == 4)
{
HyperLink hlVisit = e.Item.FindControl("hlVisit") as HyperLink;

if(hlVisit != null)
{
hlVisit.Visible = false;
}
}
}
}

=====using button's click event=========

private void btnSubmit_Click(object sender, System.EventArgs e)
{
DataGridItem dgi = dgLink.Items[4];
HyperLink hlVisit = dgi.FindControl("hlVisit") as HyperLink;

if(hlVisit != null)
{
hlVisit.Visible = false;
}
}


Hope also helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top