Solved

Need to redirect to another page from a dropdown list selection

Posted on 2004-08-10
12
265 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
Industry Leaders: 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

739 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