user6558380
user6558380

Reputation:

Asp button to open new page without submit

I have this aspx script for the button for pop-up:

<asp:Button ID="btnNewEntry" Text="Post Code Search" OnClick="btnNewEntry_Click" runat="Server" target="_blank"/>

and behind:

protected void btnNewEntry_Click(object sender, EventArgs e)
{


 ClientScript.RegisterStartupScript(this.Page.GetType(), "", "window.open('../search/postcode_search/Default.aspx?code="+ p +"','Post Code Search','width=800,height=300,left=100,top=100,resizable=yes'); popup_handle.focus();", true);

}

But as the button is clicked the pop-up is opened but the parent page is refreshed. Why it's so? any work around?

Upvotes: 0

Views: 562

Answers (4)

user6558380
user6558380

Reputation:

So as advised, i decided to go to client side. Here is my script:

<button  onclick="OpenPopup()" type="button">Post Code Search</button>

and javascript code above it:

    <script>

 function OpenPopup() {
    var getQueryString = function ( field, url ) {
    var href = url ? url : window.location.href;
    var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
    var string = reg.exec(href);
    return string ? string[1] : null;
};
var str = getQueryString('code');
    if (str != null) {
    p = str.replace(/%20/g, ' ');
        }
    else { 
    p = "";
    }
    var url = "../search/postcode_search/Default.aspx?code=" + p;
    window.open(url, "Post Code Search", "toolbar=no, location=no,status=yes,menubar=no,scrollbars=yes,resizable=no, width=750,height=400,left=430,top=100");
    return false;
    }


</script>

so basically what it does, it grabs parameter from parent window url and adds to popup window url. My parent window url:

/Customer.aspx?code=V6E%20111&firstname=MyName

I hope it helps for others as well. Thanks for help guys.

Upvotes: 0

A_D
A_D

Reputation: 189

Try this.Updated one.

<asp:Button ID="btnNewEntry" Text="Post Code Search" OnClick="btnNewEntry_Click" runat="Server" target="_blank"
OnClientClick="javascript:window.open('../search/postcode_search/Default.aspx?code=+ p','Post Code Search','width=800,height=300,left=100,top=100,resizable=yes').focus();return false;"/>

Issue was due to "+ p +".

Upvotes: 0

fizch
fizch

Reputation: 2609

Perhaps a little explanation of what is actually happening with your code will help. We have already discussed the auto postback, so you know that clicking that button will send the event back to the server. The page initializes again and reloads the view state and all of the posted data. After that, the button click event is handled.

At this point, your code writes the window.open script to the page. Keep in mind this is no where specific. This is just script that is added some where on the page and executed. The view state then gets updated and the page is sent back to the client. If the user reloads the page, that script is going to execute again.

Your best bet is going to be converting that to a client side button only. Find a way to get the necessary data back from the server before loading your popup. The easiest way to do that is make an AJAX call and open your pop up on success from your end point.

Upvotes: 1

GANI
GANI

Reputation: 2049

Page got refreshed when a request to server is made, and server controls like Button has a property AutoPostback = true by default which means whenever they are clicked a trip to server will be made. Set AutoPostback = false for insert button, and this will do the trick for you.

 or     

Add OnClientClick="return false;" ,

<asp:button ID="btninsert" runat="server" text="Button" OnClientClick="return false;" />

Upvotes: 1

Related Questions