Noobie question here, but I've created an image button and I want to make it when the user clicks on it, it would open up a new browser or a new tab to let's say "www.google.com" The website address may change depending what it is pulling from the database
so it will probably be a string variable in the code behind.
I've tried the following, only seems to work when I run it on my computer (the one creating the code) but when I upload it to the server, it doesn't work. I think I am using the wrong code to access the client side.
Thanks all for the reply. But the URL may not always be something as direct as
www.google.com it may change base on a String variable that is located in the code behind. Is there a way to pass the variable over to the Javascript?
Private Sub BtnSave_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles BtnSave.Click
'Generates the message:
Dim strMessage As String
If (Me.CheckBox1.Checked()) Then
strMessage = "The data was saved."
Else
strMessage = "The data was NOT saved."
End If
'finishes server processing, returns to client.
Dim strScript As String = "<script language=JavaScript>"
strScript += "alert(""" & strMessage & """);"
strScript += "</script>"
If (Not Page.IsStartupScriptRegistered("clientScript")) Then
Page.RegisterStartupScript("clientScript", strScript)
End If
End Sub
gmcoder
Member
4 Points
19 Posts
Opening a new browser or tab in C# code behind
Aug 12, 2009 01:25 PM|LINK
Noobie question here, but I've created an image button and I want to make it when the user clicks on it, it would open up a new browser or a new tab to let's say "www.google.com" The website address may change depending what it is pulling from the database so it will probably be a string variable in the code behind.
I've tried the following, only seems to work when I run it on my computer (the one creating the code) but when I upload it to the server, it doesn't work. I think I am using the wrong code to access the client side.
System.Diagnostics.Process.Start(www.google.com);
Tomguta
Member
138 Points
29 Posts
Re: Opening a new browser or tab in C# code behind
Aug 12, 2009 01:31 PM|LINK
You can't access clients computer directly. You can for example attach some javascript to you response to open new window.
You you can open page in new window as user clicks some the image button and in code behind redirect the content to your page.
I'll try to find some examples.
if (MyPost.IsUseful()) MyPost.MarkAsAnswer();
Tomguta
Member
138 Points
29 Posts
Re: Opening a new browser or tab in C# code behind
Aug 12, 2009 01:33 PM|LINK
I've found another topic on this forum, that deals with same problem
http://forums.asp.net/t/431102.aspx
if (MyPost.IsUseful()) MyPost.MarkAsAnswer();
getchinna_sv
Star
12043 Points
2096 Posts
Re: Opening a new browser or tab in C# code behind
Aug 12, 2009 01:35 PM|LINK
void page_load(object s, EventArgs e) { if (!IsPostBack) { button1.Attributes.Add("onclick", "window.open('help.htm', '', '', 'height=200,width=400'"); // or alert user //button1.Attributes.Add("onclick", "alert('You clicked the button')"); // or whatever //button1.Attributes.Add("onclick", "anyJavaScriptFunction"); } }DarrellNorto...
All-Star
86723 Points
9643 Posts
Moderator
MVP
Re: Opening a new browser or tab in C# code behind
Aug 12, 2009 01:36 PM|LINK
Correct - you cannot use Process.Start in a web page.
In your HTML hyperlink, just add target="_blank" to the <a> tag.
For example:
<a href="www.google.com" target="_blank">Click here to open a new Window or Tab</a>
The problem is the ASP.NET ImageButton does not have a "target" property. Here is one workaround:
http://forums.asp.net/p/1261278/2358414.aspx
Or here is another:
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
RamchanderP
Participant
1107 Points
242 Posts
Re: Opening a new browser or tab in C# code behind
Aug 12, 2009 01:39 PM|LINK
Hi
you can use javascript to open a new browser
Button_name.Attributes.Add("onclick", "javascript:window.open('www.google.com')");
Hope this helps!
SSA
Star
9370 Points
1577 Posts
Re: Opening a new browser or tab in C# code behind
Aug 12, 2009 01:43 PM|LINK
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><asp:Button </div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">ID="DoImport" </div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">runat="server" </div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">OnClientClick="javascript:window.open('http://www.google.com', '', 'menubar=1,resizable=1,fullscreen=yes, scrollbars=auto');" </div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">Text="Open Catalog"</div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste">/></div>
Try this:
<asp:Button
ID="DoSearch"
runat="server"
OnClientClick="javascript:window.open('http://www.google.com', '', 'menubar=1,resizable=1,fullscreen=yes, scrollbars=auto');"
Text="Open Google"
/>
SSA
Star
9370 Points
1577 Posts
Re: Opening a new browser or tab in C# code behind
Aug 12, 2009 01:56 PM|LINK
For passing URL from Code behind, write java script fucntion to open window and pass parameter through Hidden field or something:
Refer this very recent post:
http://forums.asp.net/t/1458101.aspx
gmcoder
Member
4 Points
19 Posts
Re: Opening a new browser or tab in C# code behind
Aug 12, 2009 05:37 PM|LINK
Thanks all for the reply. But the URL may not always be something as direct as www.google.com it may change base on a String variable that is located in the code behind. Is there a way to pass the variable over to the Javascript?
Tomguta
Member
138 Points
29 Posts
Re: Opening a new browser or tab in C# code behind
Aug 12, 2009 05:50 PM|LINK
Private Sub BtnSave_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles BtnSave.Click 'Generates the message: Dim strMessage As String If (Me.CheckBox1.Checked()) Then strMessage = "The data was saved." Else strMessage = "The data was NOT saved." End If 'finishes server processing, returns to client. Dim strScript As String = "<script language=JavaScript>" strScript += "alert(""" & strMessage & """);" strScript += "</script>" If (Not Page.IsStartupScriptRegistered("clientScript")) Then Page.RegisterStartupScript("clientScript", strScript) End If End SubHere you can find inspiration. Source = http://aspnet.4guysfromrolla.com/articles/021104-1.2.aspx
if (MyPost.IsUseful()) MyPost.MarkAsAnswer();