Question

I have an aspx webforms page with a repeater built through a user control. Each repeater item has a link button. What I want to happen is that when the LinkButton (in the repeater on page A's user control) is clicked, the url is opened in a new tab, and a hidden id next to that LinkButton is passed (according web development best practices for security if possible) to the aspx page (page B) in the new tab. 

Both pages A and page B are in the same application.

The intent of what I described above is so that the user can easily return to their search results after returning from the URL opened by clicking on the LinkButton.

I am open to ideas on how to do this that are closer to standard best-practice methods.


So far, I have tried:

1)     cross-page posting – this worked for passing the id, but not for opening in a new tab.

2)     Setting the PostBackUrl to page B's url, setting the  Page.Form.Target="_blank" with OnClientClick calling javascript to set the hidden id from the user control to the value of an html hidden input on page B and also. 

3)     I also tried using window.open("page B url", "_newtab") in OnClientClick.

  • a) So far, the only method that worked correctly was the 2nd one from the 3 different methods above. 

    However, after page B is loaded in the new tab, I don't know how to reset page A's Page.Form.Target back to what it was previously before setting it to "_blank"

  • b) The methods that I have tried, to no avail, to reset the Page.Form.Target have been:

    • 1) Resetting the target in page A's Page_Load where IsPostBack == true --> that caused Page B to load with the same content as Page A.

    • 2) Resetting the target in page A's user control's Page_Load --> same result as method 1

    • 3) Resetting the target in page A’s user control’s LinkButton’s OnUnLoad in page A's user control --> same result as method 1

    • 4) Resetting the target in javascript through the LinkButton’s OnClientClick --> didn’t work

    • 5) Resetting the target in page B's Page_Load using a public variable from page A containing a reference
      to page A's form (similar to what can be done through cross-page posting) --> didn’t work. 


What I am thinking about trying next is:

1)     Wrapping another user control on page A to display page B's content, in an asp Panel (Panel B)

2)     Put page B’s content into the new user control page

3)     Wrapping the search results content on page A in an asp Panel (Panel A).

4)     When the LinkButton in the repeater on the new user control is clicked, the search results content in Panel A will be hidden, and Panel B will be shown.

5)     When the user wants to return to the search results, they will click on a ‘Return to Search’ LinkButton in Panel B’s content, and then Panel B will be hidden, then content of Panel B will be cleared, and Panel A will be shown again.


I'm not yet sure if that will work though.

It doesn't seem like this should be that difficult.

It is a straight-forward concept, and I would think is a fairly common situation in web development.

I feel like Wiley Coyote trying to catch the Road Runner because I come up with elaborate intelligent, thought-out plans that all completely fail.

I am now holding up a little sign that says, "Help!

Was it helpful?

Solution 2

I actually got this figured out.

I figured it out through a combination of the marked-answer on this post, How to Open new tab when we click on LinkButton, and the marked-answer on this post, Is it possible add click event to hyperlink?.



My Repeater ItemTemplate in the user control's repeater looks similar to this:

<asp:HiddenField ID="hfId" runat="server"  
    Value='<%# Eval("Id") %>'/>
<asp:HyperLink ID="myLink" runat="server" 
    Text='<%# Eval("Name") %>' 
    NavigateUrl="/myUrl.aspx" 
    Target="_blank"  />
<asp:Button ID="btnSubmit" runat="server" 
    Text="Submit" 
    OnClick="BtnClick"
    Style="display: none;" />



This is my code in the ItemDataBound of the repeater:

protected void RptrItemDataBound(object sender, RepeaterItemEventArgs e)
{
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  {
    var myId = "";

    var myNameLink = e.Item.FindControl("myLink") as HyperLink;

    if (myNameLink != null)
    {
      var submitButton = e.Item.FindControl("btnSubmit") as Button;

      if (submitButton != null)
      {
        var submitButtonClientId = submitButton.ClientID;

        myNameLink.Attributes.Add("onclick", "onNameClick('" + submitButtonClientId + "')");
      }
    }
  }
}//end RptrItemDataBound



The javascript code:

<script type="text/javascript">
  function nameClick(buttonId)
  {
    document.getElementById(buttonId).click();
  }
</script>



And here is the BtnClick C# code:

protected void BtnClick(object sender, EventArgs e)
{
  var btnSelect = sender as Button;

  if (btnSelect == null)
  {
    return;
  }

  var myListItem = (RepeaterItem)btnSelect.DataItemContainer;

  if (myListItem != null)
  {
    var hfId = myListItem.FindControl("hfId") as HiddenField;

    if (hfId != null)
    {
      var intId = int.Parse(hfId.Value);

      Session["selectedId"] = intId;
    }//end if (hfId != null)
  }//end if (myListItem != null)
}//end btnClick

OTHER TIPS

I had the same issue resolve by the following code you just try this in ur HTML page for a button in GRIDVIEW:

 <asp:LinkButton ID="LinkButton1" runat="server" Text="View" CommandArgument='<%# Bind("ref") %>'
   OnClick="LinkButton1_Click" OnClientClick="document.forms[0].target ='_blank';">View</asp:LinkButton>***
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top