简体   繁体   中英

How do I use the hyperlink field in gridview, a new pop-up, to link back to my main page

I have created a page with a button that opens up a new page, a pop-up if you will.

btnToolbarSearch.Attributes.Add("onclick", "window.open('DagbokSearch.aspx','','height=600,width=600');return false");

On this new page that opens up I have a gridview where you get the below info. (You do a search on the "from date" to the "to date" and get the records in between.)

在此处输入图片说明

The first column where it says "Gå till" is a link

<asp:HyperLinkField DataNavigateUrlFields="Foretag" 
                    DataNavigateUrlFormatString="userProfile.aspx?ID={0}" 
                    Text="Gå till" />

I would like this link to get me back to the previous page and open up the object with the corresponding id, I'm not sure how to accomplish this. Maybe there is a better way then the one I'm using but I'm still learning.

You can set its NavigateUrl property in the Rowdatabound event of the gridview. Like;

protected void gvDogBok_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           ((HyperLink)e.Row.Controls[1].Controls[1]).NavigateUrl = "~/userProfile.aspx?ID="+((YourType)e.Row.DataItem).ID+"";
         }
    }

I'm not sure if what you are asking can be done. I would advice you to use a floating div popup instead. This way you dont have to leave the current page and go to a new tab. This should solve your problem and does avoid problems with popup blockers.

Here are some examples: http://www.javascripttoolbox.com/lib/popup/example.php

Use this one

protected void gvDogBok_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       ((HyperLink)e.Row.Controls[1].Controls[1]).Attribute.Add("onclick", "window.opener.location =userProfile.aspx?ID="+    ((YourType)e.Row.DataItem).ID+"; window.close();";
     }
}

You can set JavaScript:window.location.replace(url); to the clientside onclick of the HyperLink. window.location.replace(url) will reload the page.

btnToolbarSearch.Attributes.Add("onclick", "var windowHandle = window.open('DagbokSearch.aspx','','height=600,width=600');return false");

and on hyperlink cleint side onclick

hyperLink.Attributes.Add("onclick", "windowHandle.close();window.location.replace(url);return false");

You should be able to use the window.opener property to get a reference to the parent window. You can then set it's URL to the selected link, and close the popup window.

Something like this should do the trick:

// Place this near your closing </body> tag
// NB Uses jQuery and event delegation
$(function() {        
    $('table').on('click', 'tr > td:first > a', function(event) {
        if (window.opener) {
            event.preventDefault();
            window.opener.location.href = this.href;
            window.close();
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM