Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Taking URL from Listbox and opening in new window

Status
Not open for further replies.

tomouse

Technical User
Aug 30, 2010
50
Hello,

I am not massively familiar with web stuff so perhaps I am missing something fundamental regarding Client vs Server side processing here.

I have an ASP listbox (runat="server") on a webpage. The user can enter URLs into this listbox (using a textbox and an ADD button). I then want to allow the user to select one of the URLs in the listbox and choose to view this URL in a new browser window (say by clicking a VIEW button). Is this possible?

I've been trying to achieve this using a javascript function on the page but can't get it to work. Is there a way? Thanks,

Tom
 
Hi jbenson,

I was playing around with document.getElementsByName and getElementByID without success. In case it's of any help, the code for the listbox is:
Code:
<asp:ListBox ID="lbAttachmentFiles" runat="server" Width="350" SelectionMode="Multiple"></asp:ListBox>
There is also an image on the form to call the function:
Code:
<img src="/img/openURL.gif" onclick="javascript:open_win();">

And then I was trying to get access to the items within the listbox like this:
Code:
 function open_win() {
 var url_add = document.getElementsByName('lbAttachmentFiles').SelectedItem.text;
 window.open(url_add,'your win', 'width=500,height=400,menubar=yes,status=yes, location=yes,toolbar=yes,scrollbars=yes, resizable=yes');
I haven't done a great deal of web stuff (can you tell?!) so I'm sure I'm making all kinds of ridiculous mistakes here. A friendly shove in the right direction would be great. Ideally I wanted to allow the users to simply double-click the URL in the listbox that they wanted to view and it would open in a new browser window/tab, but that seemed even more tricky.
 
webforms creates some wicked client id's. so there is a good chance the control on the client is not named "lbAttachmentFiles". it might look something like "mymaster$something_ctrl1$lbAttachmentFiles". to pass the client id to the browser would look like this
Code:
<%=myControl.ClientID%>
once you have the control you can access the value of the control and navigate to the url by setting the window's location.
Code:
var url = document.getElementByID('<%=myControl.ClientID%>').value;
window.location = url;
this assumes you have a list box like this
Code:
<select id="...">
   <option value="[URL unfurl="true"]http://www.google.com">google</option>[/URL]
   <option value="[URL unfurl="true"]http://www.yahoo.com">yahoo</option>[/URL]
   <option value="[URL unfurl="true"]http://www.msn.com">msn</option>[/URL]
   <option value="mypage.aspx">My Page</option>
</select>
javascript varies between browsers. one way to eliminate (reduce) the variances is using a js library like jquery to abstract the browser from you're code.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top