Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Want to Add Hyperlink to Dynamically Created Gridview 1

Status
Not open for further replies.

DotNetDunce

Programmer
Aug 23, 2001
47
US
I have a dynamically created gridview that I would like to add a column to. When I add this column I want it to contain hyperlinks linking to a detail page. Does anyone know how to do this in an easy to understand way?

Thanks,

Melinda
 
dynamic controls must be generated in the Init stage on every server ca. the links will be geneated in the GridView_RowDataBound event. should look something like this:
Code:
public class MyPage : Page
{
   protected override void OnInit(EventArgs e)
   {
      base.OnInit(e);

      GridView grid = new GridView();
      grid.RowDataBound += new GridViewRowDataBoundEventHandler(grid_RowDataBound);
      grid.DataSource = //get data (assume datatable);
      grid.DataBind();

      this.Controls.Add(grid);
   }

   private void RowDataBound(object sender, RowDataBoundEventArgs e)
   {
      if(e.Row.RowType == RowType.DataRow)
      {
         //assume data table was bound
         DataRowView row = (DataRowView)e.Row.DataItem;

         HyperLink link = new HyperLink();
         link.Text = row["Link Text"];
         link.NavigateUrl = row["URL"];

         e.Row.Cells[0].Controls.Add(link);
      }
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top