ASP.net Dropdown help

renvilo

Well-Known Member
Joined
Jan 28, 2008
Messages
485
Hi guys,

I have a ASP.net (VS2005) website that I am working on. I also have a SQL 2005 DB in the back that is getting all the info.

I have 2 drop down boxes.

1. Department
2. Mnemonic

Is there any way to say if in the Department Dropdown box the user selects "Finance" that the second dropdown box (Mnemonic) auto selects the correct Mnemonic?

The list is also in a SQL DB (tblDepartment) with the 2 columns "Department" and "Mnemonic)

So for every department there is a relative Mnemonic.

:confused:
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
Do you want that auto selection to happen client or server side? In other words, should the selection happen before a page postback or after?

If you don't mind the postback inbetween, you could easily check the selected value on server-side and set the appropriate value for the other dropdown. Just set the autopostback property of the dropdown list to "True" for that to work and associate a server-side event handler for the OnChange event of the dropdown list.

If, however, you want it to happen client-side, you'll have to delve into a bit of JavaScript. You could associate the Value property of the Department dropdown list to look something like "DepartmentId|MnemonicId" and then in an on-change or on-click event handler in JavaScript you could parse the selected value to retrieve the MnemonicId and set the selected value of the Mnemonic dropdown accordingly.

Easy, huh? :p
 

renvilo

Well-Known Member
Joined
Jan 28, 2008
Messages
485
I would like it if the client can select the department and the next dropdown autoselects the correct mnemonic. I'll read up on the pipe character and see what I can do.

tx
 

dequadin

Expert Member
Joined
May 9, 2008
Messages
1,434
I would like it if the client can select the department and the next dropdown autoselects the correct mnemonic. I'll read up on the pipe character and see what I can do.

tx

The piping character really doesn't do anything.

Look at this C# example
Code:
string dropDownValue = "01|02";
string secondValue = dropDownValue .Substring(eg.IndexOf('|') + 1, dropDownValue .Length - dropDownValue .IndexOf('|') - 1);

So it allows you to efficiency delimit strings and substring out the values safely. So basically it's a trick to store multiple values in a string.

Hope that makes sense.
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
I would like it if the client can select the department and the next dropdown autoselects the correct mnemonic. I'll read up on the pipe character and see what I can do.

tx

Dude, as dequadin said, the pipe character doesn't really "do" anything. Your just as well off using a dollar sign or a dash. It'll make no difference, really. Just follow the train of thought I specified in my reply and you'll be alright. ;)
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
Code:
string dropDownValue = "01|02";
string secondValue = dropDownValue .Substring(eg.IndexOf('|') + 1, dropDownValue .Length - dropDownValue .IndexOf('|') - 1);
A better option would be something like:
Code:
string dropDownValue = "01|02";
string[] arrayOfValues = dropDownValue.Split('|');
string firstValue = arrayOfValues[0];
string secondValue = arrayOfValues[1];
In that way, you have an array of all the values that you can use to your coding-heart's desire. :p
 

dequadin

Expert Member
Joined
May 9, 2008
Messages
1,434
Obviously you could use split :)

I just went a bit "old school" to illustrate the point :) Forgive my ignorance but is there a split equivalent in JavaScript?
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
What's up with that anyway? Wasn't his request urgent enough? Maybe he found a solution in the "pipe character"...
 
Top