jquery - how to add options in <select> tag at page load dynamically using c#? -


i populating cascaded <select> elements database using jquery ajax. there 1 problem: unable add default "select program" option @ top first <select> element dynamically. c# code doing nothing, used jquery prepend method , adds value; not show when page loads. instead of first value gets database displayed first. code retrieve data database, , calling @ page load:

   public void getprograms()     {         string cs = configurationmanager.connectionstrings["dbcs"].connectionstring;         using (sqlconnection con = new sqlconnection(cs))         {             sqlcommand cmd = new sqlcommand("spgetprograms", con);             sqldataadapter adp = new sqldataadapter(cmd);             dataset ds = new dataset();             adp.fill(ds, "programs");             con.open();             program.datasource = ds;             program.datavaluefield = "progid";             program.datatextfield = "progname";             program.databind();             con.close();         }     } 

these screen shots of page:

page loaded without select program option displayed

select option there not selected default

this jquery code

   $("#program").prepend(new option("select program", "0")); 

you can @ server-side itself

program.items.insert(0, "select program"); 

update op wants client side:

<select id="program">       <option>option 1</option>       <option>option 2</option>       <option>option 3</option>     </select> 

jquery:

$(document).ready(function(){    $("select#program").prepend("<option value='0'>select</option>");   $("select#program").val("0");   //alert($('option').length); }); 

fiddle - https://jsfiddle.net/wj00esyz/


Comments