Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET HTML Webmatrix
var ch;
<select name="cmbStatus">
    <option value= 'I'>impossible</option>
    <option value= 'U'>unvisited</option>
    <option value= 'P'>priced</option>
    <option value= 'W'>in progress</option>
    <option value= 'R'>ready</option>                          
</select>
 
 
How can I select the appropriate option programmatically based on the value of ch? (For example if value is 'W' select 'in progress' in the select control )
Posted 29-Dec-12 3:26am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I'm not sure about having understood your question, but if your goal is to select an option according with the value of a variable, this accomplishes your expectations:
<select name="cmbStatus">
    <option value= 'I' @(ch == "I" ? "selected = 'selected'" : "")>impossible</option>
    <option value= 'U' @(ch == "U" ? "selected = 'selected'" : "")>unvisited</option>
    <option value= 'P' @(ch == "P" ? "selected = 'selected'" : "")>priced</option>
    <option value= 'W' @(ch == "W" ? "selected = 'selected'" : "")>in progress</option>
    <option value= 'R' @(ch == "R" ? "selected = 'selected'" : "")>ready</option>
</select>
  Permalink  
Comments
cs101000 at 10-Jan-13 4:54am
   
Thanks a lot. That was exactly what I was looking for.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

How can I select the appropriate option programmatically based on the value of ch
Using JavaScript you can set like:
function setDropDownSeletedValue(selectedValue)
{
    var myCB=document.getElementById('myCB');
    myCB.value=selectedValue;
}
 
Now, set value from code behind or JS directly:
//Get value from DB
string valueFromDB = "W";//e.g value in DB is 'Second'
string script = string.Format("setDropDownSeletedValue('{0}')", valueFromDB);
ClientScript.RegisterStartupScript(this.GetType(), "KeyValue", script, true);
  Permalink  
Comments
cs101000 at 10-Jan-13 4:57am
   
Thank you. But I Have 2 questions about it. 1.What if the user has turned off javascript on his browser? 2.I didnt understand the second code you provided. Is that client-side code?
Sandeep Mewara at 10-Jan-13 8:42am
   
1. Today almost everyone has JS enabled. Client side is must.
2. Second part was server side code of how to inject JS via code behind.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,745
1 OriginalGriff 6,696
2 CPallini 5,315
3 George Jonsson 3,599
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100