-1

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!

Bryan
  • 3,629
  • 2
  • 28
  • 27
  • 1
    Does it have to be in a hidden input? Can't you just use a query string or #? – LiverpoolsNumber9 Mar 25 '13 at 16:14
  • no, we couldn't use a query string, because the query string could potentially be changed on the client-side, and that would cause a lot of data-issues. – Bryan Mar 25 '13 at 18:03
  • 1
    So could a hidden field couldn't it? – LiverpoolsNumber9 Mar 26 '13 at 00:02
  • Yes, I believe so. We ended up using a session variable. But I wonder what is the industry best-practice for passing SQL db row-Ids from one ASP.Net webforms page to another in the same application? Is passing them as query string parameters considered acceptable? I think I'm going to ask this as a separate question, and get some clarification on that subject. Thanks for your comments though. – Bryan Mar 26 '13 at 15:41
  • 1
    No problem. It is absolutely fine. Have you ever done an ASP.NET MVC? IDs are passed all the time as part of the url or query string. Like this page for example - "15619556" is the ID of the post. I don't think it's worth worrying about exposing data to end users like that. If you were giving out passwords or web.config settings, maybe! – LiverpoolsNumber9 Mar 27 '13 at 15:36

2 Answers2

1

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>***
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Chakki
  • 21
  • 4
0

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
Bryan
  • 3,629
  • 2
  • 28
  • 27
  • We've since ditched the webforms project related to this question, and are in the process of completely rewriting it in ASP.Net MVC. It's been very refreshing making the change from webforms to MVC. – Bryan Jun 20 '13 at 16:27