Wednesday 15 August 2012

c# - get the ID of a selected value in the drop down list -



c# - get the ID of a selected value in the drop down list -

i have album table id_album attribute. , songs table id_song attribute , id_album foreign key attribute. in aspx page have drop downwards list of album names selected id , utilize id in songs table add together new songs ofthat album *depending on id) have done far returns me id_album 1 whenever album name selected drop downwards list. in other words, songs added album of id=1.what going wrong??

this aspx page:

public partial class newsongs : system.web.ui.page { protected void page_load(object sender, eventargs e) { musicstorebl bl = new musicstorebl(); var info = bl.getalbums(); //getalbums function returns id_album, albumname albums.datasource = data; //albums html dropdownlist albums.datatextfield = "albumname"; albums.datavaluefield = "id_album"; albums.databind(); } protected void save_click(object sender, eventargs e) { song song = new song(); //song get/set model song.id_album = int.parse(albums.selectedvalue); song.songname = songname.text; song.songauthor = author.text; song.musicartist = songartist.text; song.genre = genre.text; musicstorebl bl = new musicstorebl(); bl.createnewsong(song); response.redirect("albumselection.aspx"); } }

you should place info binding code in page load event handler within next if statement:

if(!page.ispostback) { musicstorebl bl = new musicstorebl(); var info = bl.getalbums(); //getalbums function returns id_album, albumname albums.datasource = data; //albums html dropdownlist albums.datatextfield = "albumname"; albums.datavaluefield = "id_album"; albums.databind(); }

this way fetch info 1 time , bind them corresponding drop downwards list control. otherwise, each time trigger postback, selecting element drop downwards list command or clicking on button , on, info bind code in page_load runs start.

so why time id=1?

the reason why every time id 1 fact every time click save code in page_load runs. info bind happens , first item of drop downwards list selected one.

c# asp.net drop-down-menu

No comments:

Post a Comment