Link to home
Start Free TrialLog in
Avatar of AmyL
AmyL

asked on

ASP.NET Button OnClick to Open New Window

I have a list of dynamically created buttons (the total number will vary). Each needs an onClick event to open a new browser window and pass in an argument based on the ID of the button.
In the creation of each button I have:
AddHandler btnView.Click, AddressOf btnViewHandler

In the btnView Handler I have:
 Public Sub btnViewHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim Btn As Button = DirectCast(sender, Button)
            Dim targetID As String = Btn.ID
            Dim targetDropDownID As String = "ddl" & targetID
 End Sub

What I need to do next in that sub is 1.) Get the selected value of the dropdown list that has the same name as the value of targetDropDownID and 2.) Open a new browser window passing in that value.

Any help would be greatly appreciated.
Avatar of third
third
Flag of Philippines image

I would approach this in javascript. the passed value can be obtained from the querystring.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<script>
function winpopup(obj){
  var ddlValue = document.getElementById('ddl' + obj.id).value;
  window.open('someurl.aspx?ddlValue='+ddlValue)
}
</script>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" />          
        <asp:DropDownList ID="ddlButton1" runat="server">
          <asp:ListItem>First Item</asp:ListItem>
            <asp:ListItem>Second Item</asp:ListItem>
            <asp:ListItem>Third Item</asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>
 
 
code-behind code
-----------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Button1.Attributes.Add("onclick", "winpopup(this)")
End Sub

Open in new window

Avatar of AmyL
AmyL

ASKER

The popup needs to know the current selected value of the associated dropdown (there is one dropdown created dynamically for each button that is created). Can you show me how I would get that value using your example?
Thanks!
Amy
var ddlValue = document.getElementById('ddl' + obj.id).value;

this line on the function extracts the value of the associated dropdown of the button. then using that value, I'm passing it as querysting on the url of the window to be opened. see below

function winpopup(obj){
  var ddlValue = document.getElementById('ddl' + obj.id).value;  //extract value of dropdown
  window.open('someurl.aspx?ddlValue='+ddlValue);                  //pass value as querystring
}


now, just extract the dropdown value from the querystring. note that i made the IDs of the dropdown and button the same only that the dropdown has a 'ddl' prefix. just adjust it to suit your needs.
Avatar of AmyL

ASKER

This seems like exactly what I need. I implemented it, and there is one little glitch..
In the winpopup function, I put alert(obj.id) so that I could see the object id that was being passed. The id looked like this:
ctl00_pagename_objectid

The same goes with my dropdowns...
To assign the id, I am using obj.ID="xyz". Is there a different way that I should be assigning the id?
Thanks!
Amy
can you also show me the generated id for the dropdown on the clientside so I can adjust the script accordingly?
ASKER CERTIFIED SOLUTION
Avatar of third
third
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AmyL

ASKER

Thanks! (Sorry it took me so long to get back to you...my computer was down for a while).