ASP.NET - Opening a new window in asp.net dropdown selected index change event.

Asked By Manu on 04-Oct-11 02:37 AM
Hi All,

I want to open a new window from asp.net dropdown selected index change event (either by code behind/javascript).
please let me know the same.

Thanks.
Reena Jain replied to Manu on 04-Oct-11 02:44 AM
Hi,

Try this and let me know

<script type="text/javascript" language="javascript">
function Show()
{
  window.showModalDialog("Page.aspx",'', '');
}
</script>
 <asp:DropDownList ID="DropDownList1" onchange="Show();"  runat="server" AutoPostBack="true">

or Try to use the below in the onselectedindexchanged event.

Page.ClientScript.RegisterStartupScript(me.GetType(),"openwindow","window.open('Page.aspx');",true)

aneesa replied to Manu on 04-Oct-11 02:46 AM

This drop-down navigation menu is similar to the one shown above but unlike the other it does not have the Go button. The moment you select any of the options you are immediately taken to that page.

                                                                                         

<xmp>
<!-- DROP-DOWN NAVIGATION  -->

<!-- This goes in the HEAD of the html file -->

<script language="JavaScript" type="text/javascript">


<!--
function gopage(theLink) {
 if (document.dropdown.theLink.value != "") {
  location.href = document.dropdown.theLink.value;
 }
}
// -->
</script>

<!-- This goes in the BODY of the html file -->

 <form name="dropdown">
   <select name="theLink" onchange="gopage(this)"> 
     <option selected value="">&lt;&lt;&lt;&nbsp; Select &nbsp;&gt;&gt;&gt;</option>
     <option value="http://www.microsoft.com/">Microsoft Corp.</option>
     <option value="http://home.netscape.com/">Netscape Corp.</option>
     <option value="http://www.macromedia.com">Macromedia Inc.</option>
     <option value="http://www.symantec.com">Symantec Corp.</option>
   </select>
 </form>
</xmp>
Anoop S replied to Manu on 04-Oct-11 02:47 AM
Try to use the below in the onselectedindexchanged event.

Page.ClientScript.RegisterStartupScript(me.GetType(),"openwindow","window.open('Page.aspx');",true)

aneesa replied to Manu on 04-Oct-11 02:48 AM

This drop-down navigation menu is similar to the one shown above but unlike the other it does not have the Go button. The moment you select any of the options you are immediately taken to that page.

                                                                                         

<!-- DROP-DOWN NAVIGATION MENU - II -->
 
<!-- This goes in the HEAD of the html file -->
 
<script language="JavaScript" type="text/javascript">
 
 
 
<!--
function gopage(theLink) {
 if (document.dropdown.theLink.value != "") {
  location.href = document.dropdown.theLink.value;
 }
}
// -->
</script>
 
<!-- This goes in the BODY of the html file -->
 
 <form name="dropdown">
   <select name="theLink" onchange="gopage(this)">
   <option selected value=""><<<  Select  >>></option>
   <option value="http://www.microsoft.com/">Microsoft Corp.</option>
   <option value="http://home.netscape.com/">Netscape Corp.</option>
   <option value="http://www.macromedia.com">Macromedia Inc.</option>
   <option value="http://www.symantec.com">Symantec Corp.</option>
   </select>
 </form>

Venkat K replied to Manu on 04-Oct-11 02:49 AM
Hi manu,

You can also assign a javascript function to the dropdown onchange as follow:
ddl1.Attributes["onchange"] = "if( !confirm('Are You sure?' ) ) return;";



Thanks
Manu replied to Reena Jain on 04-Oct-11 02:54 AM
Hi,

I already have the OnSelectedIndexChanged, is getting called in code behind and before that I want to open a window in a particular selected index value. how can i do that.

Thanks.
dipa ahuja replied to Manu on 04-Oct-11 02:56 AM
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
  string item = DropDownList1.SelectedItem.ToString();
 
  //server code
 
  Response.Redirect("page1.aspx?id='" + item + "'");
 
  // OR javascript
 
  Response.Write("<script>window.open('page1.aspx?id='" + item + "')");
}
Reena Jain replied to Manu on 04-Oct-11 03:35 AM
hi,

you want to open a new window from server side...then....

%@ Page Language="C#"%>
<%@ Import Namespace="System.IO" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
  }
 
now try this and let me know
  protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
  {
    ClientScript.RegisterStartupScript(this.GetType(),"OpenWindow","window.open('Default2.aspx?p="+ddl1.SelectedValue+"');",true);
  }
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <div>
  <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl1_SelectedIndexChanged">
  <asp:ListItem Value="1">Reena</asp:ListItem>
  <asp:ListItem Value="2">jain</asp:ListItem>
  <asp:ListItem Value="3">Eggheadcafe.com</asp:ListItem>
  </asp:DropDownList>
  </div>
  </div>
  </form>
</body>
</html>

Hope this will help you
Manu replied to Reena Jain on 04-Oct-11 03:39 AM
Same thing I have done but I am using 'ShowModalDialog' instead of window open.

string strScript = "";

strScript = strScript + "window.showModalDialog('Page.aspx', null,'center:yes;dialogHeight:350px;dialogWidth:800px;status:no');";

Page.ClientScript.RegisterStartupScript(this.GetType(), "openwindow", strScript, true);

Thanks.