Solved

Need to redirect to another page from a dropdown list selection

Posted on 2004-08-10
12
266 Views
Last Modified: 2010-04-17
I have a server control called StateList.cs. It renders a dropdown menu on the webpage with 2 choices, CA and FL. If the user clicks on CA in the list, I need to redirect him to another web page. ( aspx\html) I tried using Responce. Redirect but it kept telling me it was not defined, etc. I am not sure how to do this. The entire code is below:


using System;
using System.Text;
using System.Web.UI;  
using System.Web.UI.WebControls;
using Aon.Pltp.BusinessServices;  

 
namespace Aon.Pltp.ServerControls
{
      ///<copyright>
      ///   (c) 2002 Aon Services Group, Inc. All Rights Reserved.
      ///</copyright>
      /// <summary>
      /// Summary description for StateList.
      /// </summary>
      public class StateList : DropDownList
      {
            public StateList()
            {
                  //Set drop down width
                  this.Width = 48;  
                        
                  //set item value and item text

                  string[] elements = {"CA", "FL"};
                  //Add item to list item
                  for (int i = 0;i < elements.GetLength(0);i++)
                  {
                        this.Items.Add(new ListItem(elements[i]));
                  }
            }





// TODO: This code is probably leftover from an earlier test that was never deleted.  
// Should experiment with commenting it out.
            public DropDownList StateSpecificlist()
            {
                  //Set drop down width
                  this.Width = 48;  
                  
                  DropDownList dd = new DropDownList();
 
                  //set item value and item text
                  User user = BusinessServices.UserState.getUser();
                  string state = user.RegionID.ToUpper();
                  string element = state;
            
                  
                  //Add item to list item
                  dd.Items.Add(element);
                  
                  return dd;
            }

      }
      
      
      

      }




      

      

0
Comment
Question by:dmullis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 3

Expert Comment

by:thecode101
ID: 11765014
I don't know if this will work for you but this script works for me:
<head>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
   eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
   if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body>
<form name="form1">
<select name="var" id="var" onChange="MM_jumpMenu('self',this,1)">
<option value="newpage.html">New Page</option>
<option value="newpage2.html">New Page2</option>
</select>
</form>
</body>
0
 

Author Comment

by:dmullis
ID: 11765052
I don't think I can put HTML or Javascript in a .cs page.
0
 
LVL 7

Expert Comment

by:suramsureshbabu
ID: 11765236
try out putting hyperlink along with the name of the state in teh array list

string[] elements = {"<a href='CA.html'>CA</a>", "<a href='FL.html'>FL</a?"};
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Expert Comment

by:vbnik
ID: 11765294
Try to add "using System.Web"
0
 

Author Comment

by:dmullis
ID: 11765404
I've added System. Web and I still can't get anything to work.

I also tried string[] elements = {"<a href='CA.html'>CA</a>", "<a href='FL.html'>FL</a?"};
It literally puts all that info in the dropdown menu.

Any other ideas?
0
 

Expert Comment

by:vbnik
ID: 11765545
On a form where you put your control write something like this (My syntax is more like VB)

If MyControl.SelectedItem = 0 then Response.Redirect ( 'CA.html')
Else Response.Redirect ('FL.html')
end

It looks like your control does not know what HttpResponse object is.
0
 

Author Comment

by:dmullis
ID: 11765629
I'm still having trouble with it. I am so new to C# and programing in general, it's probably something simple I'm doing or not doing. I didn't write this page. You are right ,It does not seem to recognise the HttpResponse object  though.

I tried if Statelist.Elements but it didn't like it.
0
 

Author Comment

by:dmullis
ID: 11766648
using System;
using System.Text;
using System.Web.UI;  
using System.Web.UI.WebControls;
using Aon.Pltp.BusinessServices;  
System. Web
 
namespace Aon.Pltp.ServerControls
{
     ///<copyright>
     ///   (c) 2002 Aon Services Group, Inc. All Rights Reserved.
     ///</copyright>
     /// <summary>
     /// Summary description for StateList.
     /// </summary>
     public class StateList : DropDownList
     {
          public StateList()
          {
               //Set drop down width
               this.Width = 48;  
                   
               //set item value and item text

             string[] elements = {"CA", "FL"};
                  //Add item to list item
                  for (int i = 0;i < elements.GetLength(0);i++)
                  {
                        this.Items.Add(new ListItem(elements[i]));


                        if (i== 1)
                              
                        {
                              
                              HttpContext.Current.Response.Redirect("http://localhost/pltp/Pages/OldVehicles.aspx");
                        }                  
                  }
            }
                  



0
 

Author Comment

by:dmullis
ID: 11766709
Oops. I accidentally sent the page above before I mean too. I added Using System.web and this code:

 if (i== 1)
                         
                    {
                         
                         HttpContext.Current.Response.Redirect("http://localhost/pltp/Pages/OldVehicles.aspx");
                    }  

This code redirects the page the second it's loaded. It doesn't wait for a dropdown selectin to be clicked. the entire code is above. Any ideas on how to only have it redirect when the value is selected?            


0
 
LVL 7

Accepted Solution

by:
jacobhoover earned 500 total points
ID: 11768676
You should be able to handle the event on the server side with:

In Page_Load:

ddlLocation.SelectedIndexChanged += new System.EventHandler(this.ddlLocationSelectedIndexChanged);

Then create your own handler in the class:

protected void ddlLocationSelectedIndexChanged(Object sender , System.EventArgs e)
{
  If ddlLocation.SelectedItem.Value = "CA"  Server.Transfer("http://localhost/CA/Index.aspx");
  If ddlLocation.SelectedItem.Value = "WI"  Server.Transfer("http://localhost/WI/Index.aspx");
}

P.S.
  Tis is all done in the CS file and you need to put AutoPostBack="True" on the dropdownlist or have a submit button the user clicks.
0
 

Author Comment

by:dmullis
ID: 11773355
Hi jacobhover. I am going to attempt doing this with javascript on an XML page as someone suggested at my work first, then if no go, come back to your suggestion. It appears what you wrote above is what I may be looking for.
0
 

Author Comment

by:dmullis
ID: 11775023
jacobhover,

I was actually able to use javascript this time but this is definitely something I need to know how to do. I am going to print out and save for future reference.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Six Sigma Control Plans
Introduction to Processes

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question