Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

create HyperLinkColumn from code behind file dynamically

Posted on 2005-04-18
7
Medium Priority
?
220 Views
Last Modified: 2008-02-01
Dear all,

I am just wondering whether I can somehow create columns of type: HyperLinkColumn from the code behind file.

Here is the situation. I have a datagrid and I successfully bind a table to it (using a dataset). I don’t know the size of the table in advance (neither the number of rows nor the number of columns). Some of the columns contain unique names of files, which are stored on the web server.  I would like to enable the user to download these files by clicking on the hyperlink.

Is this possible? Any pointers (e.g. C# code) would be very much appreciated. Many thanks in advance.

Cheers,

Chris
0
Comment
Question by:csetzkorn
  • 3
  • 2
  • 2
7 Comments
 
LVL 9

Expert Comment

by:Type25
ID: 13805185
Something like:

private void Item_Databound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            {
                  
                  
                  for (int i=0; i<e.Item.Cells.Count; i++)
                  {
                        try
                        {
                              HyperLink lnk = (HyperLink)e.Item.Cells[i].Controls[1];
                              lnk.NavigateUrl = lnk.Text;
                        }
                        catch (Exception ex){}
                  }
                                  }
0
 
LVL 6

Accepted Solution

by:
dharmesh_amity earned 750 total points
ID: 13806000
You will have to this in page_load event, before binding the datagrid. You get the table from the database and find out how many columns you need to add.

HyperLinkColumn hyp = new HyperLinkColumn();
//Set all the required properties here
dg.Columns.Add(hyp);

0
 
LVL 9

Expert Comment

by:Type25
ID: 13806067
Sorry, yes if it's dynamic like that, dharmesh is right
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:csetzkorn
ID: 13806441
Hi dharmesh_amity,

Could you please tell me how I can assign a HyperLinkColumn to a particular column in the table I bind/bound to the datagrid.

Many thanks.

Chris
0
 
LVL 6

Expert Comment

by:dharmesh_amity
ID: 13806754
if you want to add the HyperlinkColumn at particular index then,

instead of Add call AddAt.

dg.Columns.AddAt(index , hyp);

you will have to set some properties of HyperlinkColumn.

hyp.DataNavigateUrlFormatString = "http://www.newpage.aspx?id={0}";
hyp.DataNavigateUrlField = "FieldName"; // Field from the datasource you want to bind.

0
 

Author Comment

by:csetzkorn
ID: 13814008
Hello again,

Ok this seems to work. I can add HyperLinkColumn and BoundColumn from the code-behind file to the datagrid. The columns also get filled with the values from a table in a dataset. Unfortunately, the columns within the table appear again in the datagrid. This means that each column appears twice. I thought that the datagrid is intelligent enough to pick each column only once from the table. Can I somehow prevent this? I hope you understand what I mean. Many thanks in advance.

Chris
0
 

Author Comment

by:csetzkorn
ID: 13814093
Ok,

I got it. I just have to use AutoGenerateColumns="false". Many thanks for you help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Integration Management Part 2
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question