简体   繁体   中英

How to open another tab on click without refresh current page in ASP.NET Web Forms?

I want to be able to click on a button which opens another tab on click. In my case this is done using two ASP.Net controls, one asp literal with <form> tags to format it as a submit button, because I need it to have some POST functionality. The code inside the asp literal looks like the following:

<input type='submit' id='openWindowButton' value = 'Open window'> 

with a given URL and _blank to open the new tab. I am using another asp control, a LinkButton this time, to run an event on click, and also to trigger the click on the submit asp literal with javascript code. It looks like this:

<asp:LinkButton ID="literalID" OnClick="DoStuff" runat="server">Do stuff</asp:LinkButton>

On DoStuff event I need to run some code, which populates the asp literal with the code mentioned above, and after it finishes, it triggers click on the submit button with openWindowButton ID like this:

function InvokeReportFormClick() {
            document.getElementById("openWindowButton").click();
            return false;
        }

The code for click is added with ScriptManager.RegisterStartupScript method.

Everything works fine this way, the LinkButton populates the literal and then clicks on it, and the literal opens a tab. The only problem is that the page is reloaded, and I want to avoid this behavior. I have tried the return false at the end of the script, but if I only return false from the javascript method, as can be seen above, the page still reloads, and I think it is because of the generated click on the asp literal .

I could add I add return false on the onclick event on the asp literal input as follows:

<input type='submit' id='openWindowButton' value = 'Open window' onclick="return false;"> 

but it stops from opening the tab I need.

I have also tried to put the asp:Linkbutton in an UpdatePanel control as in this sample:

<asp:ScriptManager runat="server" ID="reportScriptManager">
</asp:ScriptManager>
<asp:UpdatePanel ID="updatePanelID" runat="server">
<ContentTemplate>

<asp:LinkButton ID="literalID" OnClick="DoStuff" runat="server">Do stuff</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>

But it still refresh the page, as I still guess from the asp literal click. And if I add the asp literal next to LinkButton , the page does not refresh, but I get an error saying that

Uncaught TypeError: Cannot read property 'click' of null.

Which I guess it happens because the page does not reload, and the asp literal Text is not loaded in page when click action occurs.

How can I stop the reload of the page, and still maintain the same functionality of these buttons? Best regards!

EDIT

I have remarked that the button of submit made from asp literal does not reload the form, as a simple manual click on it only opens the new tab. So the main problem in this case is only to make the asp LinkButton not trigger the page reload.

In the .cs file I am using this method to register my script to be runned after click on the LinkButton, as is suggested on asp.net forum :

RegisterStartupScript(Page page, Type type, string key, string script, bool addScriptTags)

This problem was fixed after I realized that I am using the wrong overload of the ScriptManager.RegisterStartupScript . I was using the method which takes the Page as first parameter (it can be found here ), and after I tried to use the method which takes as parameter

The control that is registering the client script block

as in msdn documentation, and sent as control the LinkButton, it actually worked. The page was not reloading anymore. I hope this can help somebody with the same problem.

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