Link to home
Start Free TrialLog in
Avatar of Vx_Chemical
Vx_Chemical

asked on

Load webpage from listbox Selection

I want to load a webpage from a listbox selection, my listbox is generated from a database.

any hints at what code i should use to accomplish this, iv searched around and havent found a C# answer for it.
Avatar of Bodestone
Bodestone
Flag of United Kingdom of Great Britain and Northern Ireland image

Assuming you have all the code already to populate the listbox from the DB in c# then you just need to write some javascript for the onchange event of the listbox.

Somethign like onchange="window.open(this.options[this.selectedIndex].value,'_top'

sorry, cut and paste blunder: onchange="window.open(this.options[this.selectedIndex].value,'_top')"
Avatar of Vx_Chemical
Vx_Chemical

ASKER

As i wrote i would like a C# answer for it :)

But thanks anyway

The code for listbox and the database is already in order,

The values in the data base

Are:

ID: 1
Location: Here

ID: 2
Location: There

i want when i select There, to have the webpage change to There.aspx
set ListBox AutoPostBack="true" to true, handle OnSelectedIndexChanged, in the event handler code use Response.Redirect("") to the page you want
ohhh i wrote listbox when i meant dropdownlist, sorry for the confusion
You would still need to set the onchange event for the listbox and the end result would be that the aspx would write out javascript to the page.

I'll see if I can find the c# property for this but the onchange text would be:

onchange="location.href=this.options[this.selectedIndex].value + '.aspx'"
Use OnSelectedIndexChanged event of dropdownlist.
In that event write
Response.Redirect(DropDownList1.SelectedItem.Text + ".aspx") ;
SOLUTION
Avatar of Hamid Hassan
Hamid Hassan
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Coagantus

When testing the code in a secluded project it worked just as it should, however when im implmenting it into my website nothing happens, ill try and paste some of the code which im using

first my aspx.cs

protected void locationDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
    Response.Redirect(locationDropDown.SelectedValue + ".aspx");

my aspx attached as cude





<td align="left" style="width: 160px">
                                                    *
                                                    Location of Access:</td>
                                                <td align="left" style="width: 179px">
                                                    <asp:DropDownList ID="locationDropDown" runat="server" AutoPostBack="True" 

DataSourceID="locationSrc"
                                                        DataTextField="Location" DataValueField="Id" Width="155px">
                                                    </asp:DropDownList><asp:CompareValidator ID="CompareValidator2" runat="server" 

ControlToValidate="locationDropDown"
                                                        ErrorMessage="Please select location of visit" Operator="NotEqual" 

ValueToCompare="5">*</asp:CompareValidator></td>

Open in new window

Location is in DataTextField. So try

Response.Redirect(locationDropDown.SelectedItem.Text + ".aspx") ;
That doesnt seem to work im afraid.

There are two options in my database, lets call them Here and There for now, one has in ID of 1 and the other of 4.

It does the Autopost back, but the page doesnt change.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great work, thanks for everyone involved, im still new at this but i learn something everyday!