1

I am working with two files, Default.aspx and Beta.aspx.

In Default.aspx I have a button that navigates to a Beta.aspx

<telerik:RadImageButton ID="btn26" runat="server" Skin="Material" Text="26" OnClick="btnConfirm_Click26">
</telerik:RadImageButton>


protected void btnConfirm_Click26(object sender, EventArgs e)
{
    string url = string.Format("Beta.aspx?year={0}&track={1}&event={2}&car=26&session{3}", hidYear.Value, hidTrack.Value, hidEvent.Value, hidSession.Value);
    Response.Redirect(url);
}

I have the Beta.aspx form tag set up like so in order to force the page to load as a new tab:

<form id="form1" runat="server" class="SmallFont" target="_blank">

The button works, and loads the page in Beta.aspx as a new tab like I had hoped for, however the Default.aspx page that the button resides on also navigates to the same URL. I want Default.aspx to always stay on the same page, what am I missing here?

MaxB
  • 428
  • 1
  • 8
  • 24

2 Answers2

2

To open a new tab, there is no need to do a server round-trip. You need to move your button onto a separate html form, skip the aspx code and handle the click event using (client-side) JavaScript, like this:

<form id="form2" class="SmallFont" action="Beta.aspx" target="_blank">
    <input type="submit" ID="btn26" Skin="Material" Text="26" />
    <input type="hidden" name="year" value="{0}" />
    <input type="hidden" name="track" value="{1}" />
</form>

or this: (which might not work as-well because of popup blockers)

<input type="button" ID="btn26" Skin="Material" Text="26" 
   OnClick="window.open('Beta.aspx?year={0}&track={1}&event={2}&car=26&session{3}', '_blank');" />

If you don't like the old-school JavaScript, there are more-modern ways, with jQuery or other frameworks, etc. HTML button onclick event

tgolisch
  • 6,549
  • 3
  • 24
  • 42
  • So my `Default.aspx` where all of the buttons live should have 2 separate forms? – MaxB Apr 01 '20 at 16:20
  • This does indeed open a separate tab. However, all of the `year, track, etc` data lives on another form and it doesn't seem that the button is able to pull in that info? – MaxB Apr 01 '20 at 16:41
  • If you are only submitting the info to Beta.aspx, you could move those fields into that form. If you need it for both, you could either copy those values into the hidden fields (js), or set the hidden fields to also exist server-side and populate them in both forms. Which would you prefer (so I can modify my example code, above)? – tgolisch Apr 01 '20 at 17:46
  • I don't really understand. The info is being submitted to beta.aspx. The form with the button is only used to pick the info. Right now the hidden fields exist in both .aspx files. I feel like there has to be an easier way to simply have the button open a new tab and perform the action. – MaxB Apr 01 '20 at 17:58
  • I agree, there must be a simpler way, but I can only know about the code that you've shown. If you give more information, we can probably find a better way. – tgolisch Apr 01 '20 at 18:03
  • I don't really know how to explain more. I have a form with several variables on it (Default.aspx). I want to navigate from this form to (Beta.aspx?year=VAR&track=VAR&.....). All I am trying to accomplish here is how to make a new tab open, and navigate to a URL that I already have. – MaxB Apr 01 '20 at 18:27
1

This should be a pretty easy problem, let's put few more details out here.

There is a form on default.aspx

<form id="form1" runat="server" target="_blank">
   <asp:Button ID="btn26" runat="server" OnClick="btnConfirm_Click26" Text="26" />
</form>

The target is _blank, i.e. the new window, or tab will be opened.

The code of btnConfirm_Click26() of default.aspx has redirect

protected void btnConfirm_Click26(object sender, EventArgs e)
{
    string url = string.Format("Beta.aspx?year={0}&track={1}&event={2}&car=26&session{3}", 0, 1, 2, 3);
    Response.Redirect(url);
}

i.e. the form of default.aspx in first tab will be submitted to a new window to the same default.aspx and then redirected (in same second tab) to beta.aspx.

default.aspx (in first tab) 
                           --> default.aspx (in new tab) --> redirects to beta.aspx

If you have no code in Page_Load() of default.aspx, it should "stay" as-is in the first tab and will not "navigate to beta.aspx". If you have code in Page_Load(), make sure you use if (!Page.IsPostBack) to execute code only once when default.aspx is loaded (and not when it is redirected to beta.aspx).

As mentioned earlier, redirect does not require to be executed on server and can be done via client script too.

user2316116
  • 6,726
  • 1
  • 21
  • 35
  • My Page_Load() on defalut.aspx is using `if (!Page.IsPostBack)` which hasn't made a difference – MaxB Apr 23 '20 at 16:49
  • When you execute my sample code as is (copy code to a brand new webform and run it), do you still have a problem of navigating first tab to "beta.aspx"? – user2316116 Apr 24 '20 at 12:16
  • Using your code above in a new file behaves as it should. New tab opens, then redirects. – MaxB Apr 28 '20 at 17:03