Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need to redirect to another page from a dropdown list selection

Posted on 2004-08-10
12
Medium Priority
?
268 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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Loops Section Overview

927 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