Solved

Need to redirect to another page from a dropdown list selection

Posted on 2004-08-10
12
259 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
Comment Utility
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
Comment Utility
I don't think I can put HTML or Javascript in a .cs page.
0
 
LVL 7

Expert Comment

by:suramsureshbabu
Comment Utility
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
 

Expert Comment

by:vbnik
Comment Utility
Try to add "using System.Web"
0
 

Author Comment

by:dmullis
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:dmullis
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now