sharpcoder Posted January 5, 2004 Posted January 5, 2004 Hi, this is my first post at this forum. :) Ok, so I have this method. The idea is that it should populate a dropdownlist("forum") , on my aspx page, with items. private void addtodropdown() { IDbCommand commForum = ConnectionFactory.makeCommand(conn, "select Xnamn, id from forumcats order by Xprioritet asc"); conn.CreateCommand(); conn.Open(); IDataReader myReader = ConnectionFactory.makeDataReader(commForum); while (myReader.Read()) { ListItem newListItem = new ListItem(); newListItem.Value = myReader.GetString(1); newListItem.Text = myReader.GetString(0); forum.Items.Add(newListItem); } conn.Close(); } I've also got this method: public void movethread(object source, EventArgs e) { if (IsAdmin()) { Response.Write(forum.SelectedIndex.ToString()); IDbCommand comm = ConnectionFactory.makeCommand(conn, "update forumthreads set Xcatid ="+ forum.SelectedItem.Value.ToString() +" where id = " + Request.QueryString["threadid"] + " "); conn.CreateCommand(); conn.Open(); comm.ExecuteNonQuery(); conn.Close(); } } The problem is that I don't manage to fetch the correct values from the dropdownlist in movethread(object source, EventArgs e). its? For example, from the followring HTML code: <option selected="selected" value="5">abc</option> ... I want to extract the value "5". I've tried SelectedIndex. Value, Value, SelectedValue and God knows what else... Quote
*Experts* Bucky Posted January 5, 2004 *Experts* Posted January 5, 2004 I'm guessing that you're calling addtodropdown() in the Page's Load event, right? If this is the case, you need to check the IsPostBack property, and only populate the dropdown list if IsPostBack is false. Otherwise, every time the page loads the dropdown list is populated and the original values are erased. Now SelectedValue should return the correct value. // in Page_Load: if (!IsPostBack) addtodropdown(); Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
sharpcoder Posted January 5, 2004 Author Posted January 5, 2004 Thank you for answering Bucky! Unfortunately (?) I'm not calling it from Page_Load but from this method (triggered by a button): public void showadminoptions(object source, CommandEventArgs e) { //some other things... addtodropdown(); } Quote
sharpcoder Posted January 6, 2004 Author Posted January 6, 2004 (edited) This should be fairly easy to solve, but I can't seem to find the right way. Anybody? Edit: I found "forum.SelectedValue.ToString()" now which worked perfectly. I've tried it before but because of another error in my code I didn't get the result I expected. I'm sorry for wasting your time. Edited January 6, 2004 by sharpcoder Quote
*Experts* Bucky Posted January 6, 2004 *Experts* Posted January 6, 2004 Hey, the time wasn't wasted; you found a solution, didn't you? That's all that matters. :) Glad you figured it out; I wouldn't have thought to try that. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
kaisersoze Posted January 8, 2004 Posted January 8, 2004 "forum.SelectedValue.ToString()" is generating error when trying to display the user from the database. one can suppress the error by error handling but anyone have an answer or is it only me who is getting this error. To generate this error follw this: 1. Select a value and save to the database from a drop downlist. 2. Try to show the use what he has selected from the database (should work fine) 3. then change manually the value in the database (this value should not be in the dropdownlist) error is generated : specified argument was out of the range of valid values. paramet name: 'blah blah' Quote Note: I think as a programmer not as a human, so use my answer at your will
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.