How to insert or add URL links inside the gridview?

How to insert or add URL links inside the gridview?

There are many ways to insert or add links inside a grid. There are many ways to display all the pages in your site in a gridview.

This is one way,

<asp:HyperLinkField DataTextField="Modules" HeaderText="Name" NavigateUrl='<% Bind("URL") %>' />

This the second way to do this,

<asp:TemplateField HeaderText="Name" SortExpression="Modules">
<ItemTemplate>
<asp:HyperLink ID="hlModules" runat="server" Text='<%# Bind("Modules") %>' />
                                                                        </ItemTemplate>                                                                   

</asp:TemplateField>

Then add this code in gridview RowCreated event,

if (e.Row.RowType == DataControlRowType.DataRow)
{
    HyperLink hl = (HyperLink)e.Row.FindControl("hlModules");
    hl.NavigateUrl = "MyPage.aspx";
}

But if you want to open the page in a new window or pop up. You need to put this code in RowDataBound event of the gridview and do like this,

if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hl = (HyperLink)e.Row.FindControl("hlModules");
switch(hl.Text)
{
case "FirstPage":
hl.NavigateUrl = "javascript:OpenFirstPage();";
break;                       

case "SecondPage":
hl.NavigateUrl = "javascript:OpenSecondPage();";
break;                       

 …

}
}

Of course you need to create the javascript to open a pop up window like this,

<script type="text/javascript" language="javascript">
var winArray = new Array()
function OpenFirstPage()
{
if (!winArray[0] || winArray[0].closed)
winArray[0] = window.open("FirstPage.aspx", "FirstPage", "width=800,height=600,toolbar=no, menubar=no, directories=no, resizable=yes, scrollbars=yes");
else
winArray[0].focus();
}

function OpenSecondPage()
{
if (!winArray[0] || winArray[0].closed)
winArray[0] = window.open("SecondPage.aspx", "SecondPage", "width=800,height=600,toolbar=no, menubar=no, directories=no, resizable=yes, scrollbars=yes");

else

winArray[0].focus();

}

</script>


You may refer to my other post or blog on how to open popup or close all popup windows.

Here are my references:
Link 0

Link 1
Link 2
Link 3
Link 3
Link 4
Link 5
Link 6

Link 7
Link 8
Link 9
Link 10
Link 11
Link 12
Link 13

Advertisements
This entry was posted in ASP.NET 3.5 And C# 3.5, ASP.NET 3.5 And JavaScript, ASP.NEt And C# 3.5 And Javascript, C# 3.5 AND ASP.NET 3.5, JavaScript. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s