Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
var ch;

HTML
<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

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:
XML
<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>
 
Share this answer
 
Comments
cs101000 10-Jan-13 4:54am    
Thanks a lot. That was exactly what I was looking for.
How can I select the appropriate option programmatically based on the value of ch
Using JavaScript you can set like:
JavaScript
function setDropDownSeletedValue(selectedValue)
{
    var myCB=document.getElementById('myCB');
    myCB.value=selectedValue;
}


Now, set value from code behind or JS directly:
C#
//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);
 
Share this answer
 
Comments
cs101000 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900