Dale Harris
asked on
Set a value of an HTML Dropdown box
Hello,
We are developing using the new Razor syntax. I have the top part of the page in C#. I have the rest of the page in HTML. My page extension is a CSHTML file.
We have a normal HTML dropdown:
<label>Food Item</label>
<select name="FoodName">
<option value="HotDog">HotDogs</op tion>
<option value="Hamburgers">Hamburg ers</optio n>
<option value="Steaks">Steaks</opt ion>
</select>
And I'm storing the value of FoodName to a variable in C#:
FoodSelection = Request["FoodName"];
Now I can put that selection into the Database and pull it back out and set it to my FoodSelection variable, but I can't figure out how to select the proper dropdown for the user to see.
For a textbox, it's easy, I just set the value to "@FoodSelection" so it looks like this:
<value = "@FoodSelection" />
But how can I do this for a Dropdown box?
I hope my question is easy enough.
I'm open to using anything that works, to include AJAX and Javascript. We already have some Javascript on the page.
-Dale
We are developing using the new Razor syntax. I have the top part of the page in C#. I have the rest of the page in HTML. My page extension is a CSHTML file.
We have a normal HTML dropdown:
<label>Food Item</label>
<select name="FoodName">
<option value="HotDog">HotDogs</op
<option value="Hamburgers">Hamburg
<option value="Steaks">Steaks</opt
</select>
And I'm storing the value of FoodName to a variable in C#:
FoodSelection = Request["FoodName"];
Now I can put that selection into the Database and pull it back out and set it to my FoodSelection variable, but I can't figure out how to select the proper dropdown for the user to see.
For a textbox, it's easy, I just set the value to "@FoodSelection" so it looks like this:
<value = "@FoodSelection" />
But how can I do this for a Dropdown box?
I hope my question is easy enough.
I'm open to using anything that works, to include AJAX and Javascript. We already have some Javascript on the page.
-Dale
sorry - a couple of typos
<label>Food Item</label>
<select name="FoodName" id="FoodName">
<option value="HotDog">HotDogs</op tion>
<option value="Hamburgers">Hamburg ers</optio n>
<option value="Steaks">Steaks</opt ion>
</select>
<script>
document.GgtElementById("F oodName"). value="@Fo odSelectio n";
</script>
<label>Food Item</label>
<select name="FoodName" id="FoodName">
<option value="HotDog">HotDogs</op
<option value="Hamburgers">Hamburg
<option value="Steaks">Steaks</opt
</select>
<script>
document.GgtElementById("F
</script>
ASKER
Hmm, oddly enough it's not working.
The ID can be different from the Name right?
I wonder what I can try instead. I know the name is being stored correctly into the DB because I was able to see it.
And I tested it out and saw that the variable was being stored correctly from the database.
I even tested this out with our "address" field and tried to set the address field (textbox) to a value.
This didn't work either.
I even changed it to:
document.GetElementById("F oodName"). value="@Fo odSelectio n";
Any ideas?
The ID can be different from the Name right?
I wonder what I can try instead. I know the name is being stored correctly into the DB because I was able to see it.
And I tested it out and saw that the variable was being stored correctly from the database.
I even tested this out with our "address" field and tried to set the address field (textbox) to a value.
This didn't work either.
I even changed it to:
document.GetElementById("F
Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the quick response.
You are the man!
-Dale Harris
You are the man!
-Dale Harris
ASKER
Great solution! Quick response!
Presuming you have the answer avaialble as "@FoodSelection", change your HTML to use an "id=FoodName" on the select so that you can reference it later.
The JavaScript below the select box should render as;
documentGetElementById("Fo
Open in new window