How to open a new page/window and send parameters by clicking a button?

Because neither Server.Transfer nor Response.Redirect has an option to open a new page/window , this is a workaround:

Failed Solutions:

I thought this is a

Smart solution

<asp:Button ID=”OpenNewPage” runat=”server” Text=”Open New Page” OnClientClick =”document.forms [0].target = ‘_blank’;“/>

But it turned out that any button I click it will open a new page, because it is under forms[0].

Or less smart solution

Which turned to be smarter than the previous one:

        <asp:Button ID=”OpenNewPage” runat=”server” Text=”Open New Page” OnClientClick =”return pageOpen();“/>

    <script type=”text/javascript”>

        function pageOpen()

        {

            window.open(“~/newPage.aspx?param1=” & TextBox1.SelectedValue & “&param2=” & TextBox2.SelectedValue)

        }

    </script>

But the problem I couldn’t figure out how to send parameters to javascipt.

I tried another approach but did not work, which is to call a javascript like this:

<%

— <asp:Button ID=”Add” runat=”server” Text=” ” OnClientClick =”document.form2.target = ‘_blank’;”/> –%>

<%

— <asp:Button ID=”Add” runat=”server” Text=” ” OnClientClick =”return pageOpen(text1.SelectedValue, text2.SelectedValue);”/> –%>

<asp:TextBox ID=”hiddenParam” runat=”server” Visible=”false”></asp:TextBox>

<%

— <asp:Button ID=”Add” runat=”server” Text=” ” OnClientClick =”return pageOpen(1, 2);”/> –%>

<%

— <asp:Button ID=”Add” runat=”server” Text=” ” OnClientClick =”return pageOpen(Text1.Text, Text2.Text);”/> –%>

<%

— <asp:Button ID=”Add” runat=”server” Text=” ” /> –%>

<%

— <asp:Button ID=”Add” runat=”server” Text=” ” OnClientClick =”return pageOpen(document.getElementById(“<%= DropDownList1.ClientID %>”),document.getElementById(“<%= hiddenParam %>”));”/> –%>

<%

— <asp:Button ID=”Add” runat=”server” Text=” ” OnClientClick =”return pageOpen();” value=’Form Checker’/> –%>

And also found a good solution here:

http://weblogs.asp.net/infinitiesloop/archive/2007/09/25/response-redirect-into-a-new-window-with-extension-methods.aspx

But could not apply it because of my novelty.

The Solution:

 

Finaly, and after 2 days searching the net and try/error approach and trying different solutions, no one work until I decided to ask asp.net WebForms forum , and the answer came to me within 10 min. from an experienced guy called Santoch :

Dim strUrl As String = String.Format(“page2.aspx?param1=” & param1.SelectedValue & “&param2=” & param2)

Page.ClientScript.RegisterStartupScript(

GetType(Page), “popup”, String.Format(“window.open(‘{0}’)”, strUrl), True)

 

This is what I should call Very smart solution. Thanks Alla then Santosh.

Learning Lessons:

1-   Search the net first before asking questions.

2-  Ask your question with what you have done and failed to achieve.

3-  Searching for the solution will open doors for new knowledge and experience. I knew a lot about javascript.

4-  Every problem has a solution, no has many solutions, just be patient and optimistic.

Thanks,

Ahmed.

Advertisements

About Ahmed

Software craftsman, programmer, developer, system/business analyst, DBA and PM.
This entry was posted in JavaScript, Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s