Kentico 12: change value of one page type field based on value of another field

444 Views Asked by At

I have a custom page type with two fields, StateName and StateCode. I would like it so that when a value is selected in either of these fields, the other field’s value changes to match.

So for example, if I select “Alabama” in the StateName field, the value of the StateCode field would automatically change to “AL.” Or if I select “CO” in the StateCode field, the StateName field would automatically change to “Colorado,” etc. Is this possible?

State Fields

(I have to keep these as two separate fields, because I need to display either the state name or the state code on the front-end depending on context. Therefore, I can’t use a single field with options like AL;Alabama because then the field only returns the value AL and I have no way of getting the display name Alabama).

I know Kentico has options for setting “dependent” fields, but that only appears to determine visibility – e.g. only display Field B if I select a particular value in Field A. I can’t find a way to actually change the selected value in one field based on the selected value in another field.

1

There are 1 best solutions below

2
On

Two control solution seems some kind of an overkill here. You can use StateInfoProvider to get what you want.

StateInfo state = StateInfoProvider.GetStateInfo("Alabama")

The other way would be to use a simple drop down control and use query like that :

select stateCode + '|'+ StateDisplayName, StateDisplayName  from cms_State

to get get code and display name as value separated by |