Problem with hyperlink control when used in DataGrid in a templated column

Hi All, 

Can someone help me solve this problem. I ama beginner to ASP.NET and I am just trying out various samples. Mentioned below is a very simple program of getting data to grid with one of the column as a Bound Hyperlink control.

I guess you are pretty busy. I had to sent you a quest earlier regarding loading the data from database to an XML file. I posted the question in yur blogs. Anyway I have another question with the datagrid control. Here it is :

I have the foll. Situation in my sample test program :

I have a single DataGrid control in an ASP.NET page
Its has 2 columns. Column 0 was converted to Template Column and the template holds a HyperLink control in the Item Template section. The next column is a Button Column.
The aim is to just list all ttile_id from titles table as Hyper Link in the grid and to get the text in the HyperLink when the user clicks the button column.
Once the button is clicked it generates a DataGrid’s ItemCommand event in which I am calling a function say AddToCart with 2 args. This function takes 2 args and has the foll. Signature :

private void AddToCart(DataGridItem item, int nIndex)

I would like to capture the text in the 1st column of the grid when the user clicks the button column .
I receive the vent very much. I am able to see the value for the NavigateUrl property of the HyperLink control but I am not able to retrieve the text in the HyperLink. The string is empty. The column very much displays all the title_id’s from the titles table. For your reference I am including the code for this function which tries to capture the text of the HyperLink from the page :

private void AddToCart(DataGridItem item, int nIndex)
foreach (Control ctl in item.Cells[0].Controls)
if(ctl.GetType() == typeof(HyperLink))
String str = ((HyperLink) item.Cells[0].FindControl("TestLink")).Text;

The reason I have included the “foreach” is I had trouble in getting the Text property of Hyperlink and hence I wanted to make sure that the Cells(0) actually contains the HyperLink Control. Also I noticed a weird thing when it entered the foreach loop. For the first time the value of the variable “ctl” is Null. It acquires the handle or reference to the HyperLink only the 2nd time. Also instead of the Text property if I refer to the NavigateUrl property it gives the correct value of the URL . The NavigateUrl and the Value for each row is filled in HTML using the DataBinder.Eval method which populates the Grid at run time using a SqlDataAdapter and a DataSet. I have tried your method prescribed in the book Prog. ASP.NET too but it didn’t give me the Text property and it was empty. Following is the portion of the HTML that I have used to pupulate the data grid dynamically binding to the data source.

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 56px; POSITION: absolute; TOP: 40px" runat="server" Width="184px" AutoGenerateColumns="False">
<asp:TemplateColumn HeaderText="TitleID">
<asp:HyperLink id="TestLink" runat="server" Width="96px" NavigateUrl= '<%# DataBinder.Eval(Container.DataItem, "title_id", "reviews.aspx?TitleID={0}") %>'>
<%#DataBinder.Eval(Container.DataItem, "title_id")%>
< /asp:HyperLink>
<asp:ButtonColumn Text="Click Here" CommandName="AddToCart"></asp:ButtonColumn>

Can someone help me by telling how do I get the text in the HyperLink in the column 0 of the grid.

Thank you all.
Narayanan K.V (AKA) Narayan
Programmer Analyst
2/10/2005 4:52:23 PM 72751 articles. 3 followers. Follow

0 Replies

Similar Articles

[PageSpeed] 15
Get it on Google Play
Get it on Apple App Store


Similar Artilces:

Using a templated control inside a databound templated control
Hi,  We have a large number of pages that display contents in a simple header - content combination. To facilitate styling, we have created a custom control 'ContentArea' that exposes two template properties: HeaderTemplate and ContentTemplate. These allow a designer to define the layout for the header and content in the aspx file. In addition, two themable properties named HeaderCssClass and ContentCssClass allow the definition of a Css class for both texts. Like this, using theming, defining the style of our site contents becomes extremely simple. As an exampl...

problem with User Control position in DataGrid template column
I have a data bound user control, which includes an Image button and a label it all works fine but in the DataGrid it only appears in the first cell.As the DataGrid is populated the image button seems to load the correct number of times for the rows but does not go down the page just loads on top of its self. I am using an external style sheet to control the label display properties I believe it is due to the position which is set as Absolute for the button and label to keep the label on top of the image and I guess what I want to do is set the position attribute to relative for the c...

What control do I use (or do I not use a control at all...)
Okay so here's the story so far. I am making a blog to be incorporated into a website. Well, I'm doing the blog from scratch. I'm using MySQL to source all the data via MySQL .NET/Connector. Now while developing the script, I used DataReader and Response.Write to show the blog topics. Now that it has come to put it under a design (I'm using a Master Page for that), I've hit the problem. Response.Write shows all the topics before any of the design elements could be incorporated. So that (more or less) is out of question. So next I looked into the data presentation controls. I looked up GridVi...

Custom control problem (Templated Control)
Hello ASP.NET community,I recently a control that is very simple, really. It's suppose to look like the UpdatePanel<asp:UpdatePanel><ContentTemplate>Stuff</ContentTemplate></asp:UpdatePanel> as you know where i have "stuff" we can put all bunch of controls and then initialise them in the code behind.In my control I do the same,        private ITemplate tmpContentTemplate = null;        [DefaultValue(null)]        [Description("The content te...

Problem Using Slider Control in Gridview Control
Hello to all,   it is urgent.   i want to use slider control in a Gridview Template Column, if someone can help me how i can get this functionality?...

Control In Control Problem
Hi, guys.I've written two web controls for reservation system.One is a Datepicker which alows user to pick a date (pretty obvious ) and another one is a Reservation which alows user to pick a location (hotel) and number of rooms and number of people in each room.Both of them inherit WebControl and implement ICallbackEventHandler and INamingContainerI bind to them list with available days... List of hotels and regions (if it's to be used for reservation in hotels chain) well... whatever...Both of them work great while I don't try to include the DatePicker in the Reservation... Her...

Control in control problems
************* Edited by moderator Adec *************** Inserted missing < code></ code> tags. Always include such tags when including code in your postings. Many readers disregard postings without the code tags. ************************************************** Here is the senario I am presented with. I will follow it with the code. Webform 1 contains User Control 1. Now, this control contains User Control 2. I have Webform 1 call a subroutine 1 in User Control 1. This sub routine, calls a subroutine 2 in User control 2. Unfortunately, VS2003 will not allow Use...

How to place a DataGrid Control with in a DataGrid Control
Hi, I want to place a DataGrid control in a DataGrid control can any one plz tell me the procedure to place the control Thanks in Advance Krisamigo <!---<asp:DataGrid ID=dg_List runat=server><Columns><asp:TemplateColumn><ItemTemplate><asp:DataGrid ID=dg_List2 Runat=server></asp:DataGrid> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> ---> Hardik Joshi...

using an upload control in a datagrid or a dataview using the edit and or insert templates.
I figured this out and wanted to share how to use an upload control in a datagrid or a dataview  and use the built in edit and insert templates to add the updated file to the record.  I also included the new record page.  This page uploads a file in a formview and redirects the page back to the gridview.  finding the controls in a gridview is different than finding them in a formsview.  So I include both pages.  I am using vs2005 and .net 2.0  and an older version of SQL There are other comments on the internet about using an upload control to s...

Datagrid Control, template columns
Hi all i am working on datagrid control. I have to columns both are template columns.The first template column contains a textbox control, the second column contains a dropdownlist. I need to bind the data to the first column fileupload control and the dropdownlist should be populated from the database. How do i go about doing this please help me.  Hi...If u want to Bind Text Box From Data Base then Bind TextBox Template as like below                          &l...

datagrid in use control has a problem
I have a datagrid which inside use control has one field and two buttonsaspx code list this. <asp:datagrid id="dgEntities" runat="server" OnItemCommand="Select_function" EnableViewState="true"autoGenerateColumns="False"  ><Columns> <asp:BoundColumn DataField="Title" HeaderText="Title" /><asp:ButtonColumn Text="Detail" CommandName="Detail" /><asp:ButtonColumn Text="Delete" CommandName="Delete" />  </Columns></asp:datagrid></DIV> when I chnage code to below, OnItemCommand="Select_function" never has been called. I don't why. ...

Unable to use Custom Control within a datagrid control
I have a datagrid and a template field to look like this <asp:TemplateField> <ItemTemplate> <customControl:Display runat="server" Description='<%#DataBinder.Eval(Container.DataItem,"Description")%>' /> <asp:Label ID="mylabel" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"ReportID")%>' /> </ItemTemplate> </asp:TemplateField> My custom control has a property called "Description" which basically is nothing but the value for a label in that control. Now, in the above code, the asp label control correctly displays the text,... controls used in custom composite controls?
I am creating an events calendar that uses the accordion to display the events for the selected day.  The problem I'm having is that the accordion is not responding to any client side events.  I would really like to find a good "How to" article for building composite controls that incorporate an control like the accordion....

using a .Net Control as activex control in VCL form
Hi , We have a to integrate a simple .Dot CSharp control which needs to be integrated into bcb2010 VCL form as a control. We will place the control on the VCL form along with other VCL controls's. I tried creating a simple .net control using a lable+textbox and trying to use in vcl form. I have made the assembly comvisible and generated tlb files and .bpl for the component. I could see the component in "ActiveX" tool palette. But when I drag the object onto VCL form and try to access its attributes I run into problems. I am not able to see .net control on the vcl at runti...

Web resources about - Problem with hyperlink control when used in DataGrid in a templated column -

Hyperlink - Wikipedia, the free encyclopedia
A hyperlink points to a whole document or to a specific element within a document. Hypertext is text with hyperlinks. A software system for viewing ...

SA Supreme Court rules Dr Janice Duffy was defamed by Google through its hyperlinks, auto-complete and ...
A FORMER SA Health researcher has won a six-year legal battle against Google, proving she was defamed by the internet giant due to the way it ...

Banned hyperlinks could cost you $11,000 a day
The Australian communications regulator says it will fine people who hyperlink to sites on its blacklist, which continues to be controversially ...

ANALYSIS - Supreme Court hyperlink decision a victory for free speech
A hyperlink by itself should never be seen as 'publication' of other content, the Supreme Court said. But when it comes to libel the court did ...

Review: HyperLink is a great, inexpensive steel bracelet for the Apple Watch
Apple's own stainless steel link bracelet is too expensive for many of us to consider. HyperLink, however, is a less expensive alternative that ...

Apple's 'audio hyperlink' tech can control devices with inaudible sonic pulses
An Apple patent filing published by the U.S. Patent and Trademark Office on Thursday describes a method of encoding hyperlinks into an audio ...

Patent troll sending out invoices for image hyperlinks
A company named Vuestar thinks everyone who links images to pages on the Web …

How Effective Hyperlinks Can Help Your Blog
... link text explain exactly where the link is leading you or does it say something generic like click here ? When I saw Writing Effective Hyperlinks ...

Gmail's Shortcut for Inserting Hyperlinks
... doesn't mean Google shouldn't improve them. Many people complained that Gmail's rich text editor doesn't have a shortcut for inserting hyperlinks ...

Scientology loss keeps hyperlinks legal
The Church of Scientology loses a courtroom battle to compel a Dutch writer and her ISP to remove postings from a Web site, in a ruling that ...

Resources last updated: 1/7/2016 12:31:54 AM