Three ways to implement hyperlinks in DataGrid

  • 2020-06-23 00:10:02
  • OfStack

1. Use the hyperlink column in DataGrid - HyperLinkColumn

Many of you will have used the hyperlink column in datagrid, which is handy and passes only one parameter by default, as shown below:


<asp:HyperLinkColumn DataTextField="CompanyName" DataNavigateUrlField="CustomerID"     DataNavigateUrlFormatString="Default2.aspx?customerid={0}"  eaderText="link">
</asp:HyperLinkColumn>

As you can see, passing 1 parameter hyperlinks is easy. So how do you pass multiple parameters?

2. Assign a value to a hyperlinked column in the ItemDataBound event of DataGrid

If an id is an Datagrid of DataGrid1, with the first column as a hyperlinked column (subscript 0), its ItemDataBound event can be written in the following form.


private void DataGrid1_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgs   e)  
  {  
      if(e.Item.ItemType==ListItemType.Pager||e.Item.ItemType==ListItemType.Header||e.Item.ItemType==ListItemType.Footer)
     {
        return;  // Determine if it is a header or footer 
     }
     else
     { 
        HyperLink link=( HyperLink)e.Item.Cells[0].Controls[0];// You can also use it here findcontrol
        link.NavigateUrl="webform3.aspx?id=XXX & name=XXX";
// If related to a column, write as      
       // link.NavigateUrl="webform3.aspx?id= " +e.Item.Cells[ Column number ].text;
      }
}

If you want to use hyperlinks in a template column, you can set the column template to LinkButton. You can have multiple LinkButton starting at 1, but id cannot be the same. We still assume that the column bit is column 1. At this point, a link for one of the LinkButton definitions can be put into the statement as follows


 HyperLink link=( HyperLink)e.Item.Cells[0].Controls[0];

replace


     LinkButton link=( LinkButton)e.Item.Cells[0]. FindControl( " LinkButton the id " );
     link.Attributes["onclick"] = "<script>location.href='XXX.aspx';</script>";

When the column has only 1 control, i.e., 1 LinkButton, it can be implemented as follows:


e.Item.Cells[0].Attributes["onclick"] = "<script>location.href='XXX.aspx';</script>";

This saves time in finding the control.

In fact, we have mentioned two methods above, one is to use HyperLinkColumn column to add NavigateUrl to ItemDataBound event, the other is to use javascript to define click event in ItemDataBound event, the latter is more flexible, only need control click event can be.

Use the HTML hyperlink tag


<asp:TemplateColumn>
  <ItemTemplate>
   <a href="Default.aspx?id='<%#(String)DataBinder.Eval(Container.DataItem,"CustomerID")%>'
                         &name='<%#(String)DataBinder.Eval(Container.DataItem,"CompanyName")%>'"> operation </a>
  </ItemTemplate>
</asp:TemplateColumn>

It works by clicking to go to Default.aspx and passing the parameters id and name, both of which are dynamically generated by data binding. This approach was successful in VS2005, but reported errors in VS2003.

There is also a clever way to do the same with the third method: assume that the second column is a hyperlink, aspx page code:


<asp:boundcolumn   DataField="ID"   Visible=False></asp:boundcolumn>  // The first 1 column 
<asp:boundcolumn   DataField=" The name of the "></asp:boundcolumn>// The first 2 column 

ItemDataBound events are as follows:


public   void   DataGrid1_ItemDataBound(object sender,DataGridItemEventArgs e)  
{           
       if(e.Item.ItemType==ListItemType.Pager||e.Item.ItemType==ListItemType.Header||e.Item.ItemType==ListItemType.Footer)
       { 
          return;// Determine if it is a header or footer 
       }
      else
       { 
          e.Item.Cells[1].Text="<a href=News.aspx?ID="+e.Item.Cells[0].Text+">"+e.Item.Cells[1].Text+"</a>";// Here you can change it to what you need;   
       }      
}


Related articles: