srithil Posted May 10, 2004 Posted May 10, 2004 I have a dropdownlist staff. I try to select the option by its value property. I get the value from the datareader correctly,but when i use the following code dr_staff.SelectedItem.value=mydr("st_staff") only the first option is selected.The correct option is not selected. anybody pls help. tx Quote
Moderators Robby Posted May 10, 2004 Moderators Posted May 10, 2004 Did you set the .DataValueField = "Some_ID_column" and .DataTextField = "Some_Display_Value" Quote Visit...Bassic Software
srithil Posted May 10, 2004 Author Posted May 10, 2004 yes.this is my dropdownlist <asp:DropDownList AutoPostBack=True runat="server" DataTextField="st_staff_name" DataValueField="St_staff_id" id="dr_staff"></asp:DropDownList> tx. Quote
srithil Posted May 10, 2004 Author Posted May 10, 2004 yes.this is my dropdownlist <asp:dropDownList AutoPostBack=True runat="server" DataTextField="st_staff_name" DataValueField="St_staff_id" id="dr_staff"></asp:dropDownList> tx. Quote
srithil Posted May 10, 2004 Author Posted May 10, 2004 This is the viewsource of my page after executing the command. The value is 16.but it is not selecting the option joe. tx <select name="pc_staff" onchange="__doPostBack('dr_staff','')" language="javascript" id="dr_staff"> <option selected="selected" value="16">Select</option> <option value="16">Joe,G</option> <option value="17">Martin,L</option> <option value="18">adsfasdf</option> </select> Quote
Arch4ngel Posted May 10, 2004 Posted May 10, 2004 Actually... I'm sure that you populate your DropDownList in the Page_Load. And I'm pretty sure that you don't verify the PostBack. You must fill the DropDownList like this : If( !this.IsPostBack ) { // FILL YOUR DropDownList } when you refill your DDL and you try to get the current value... it'll always return the first one. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
srithil Posted May 10, 2004 Author Posted May 10, 2004 yes.i am loading the dropdownlist only by checking ispostback method.here ismy code in the pageload event. If Not Page.IsPostBack Then sql3 = "select * from Nt_St_staff" mycon3 = New SqlConnection(tq()) mycon3.Open() mycmd3 = New SqlCommand(sql3, mycon3) mycmd3.CommandType = CommandType.Text mydr3 = mycmd3.ExecuteReader() dr_staff.DataSource = mydr3 dr_staff.DataBind() mycon3.Close() End If thanks Quote
Arch4ngel Posted May 10, 2004 Posted May 10, 2004 Oh... by the way... you have 2 value 16. The first is called "Select" and the other is joe try choosing another value for the first line. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
srithil Posted May 10, 2004 Author Posted May 10, 2004 No.this is the viewsource of my page when it loads.but i click a button and try to select the option 16 or 17 say for eg. in my click event.then it selects only the first select option. <select name="dr_staff" onchange="__doPostBack('pc_staff','')" language="javascript" id="dr_staff"> <option selected="selected" value="15">Select</option> <option value="16">Joe,G</option> <option value="17">Martin,L</option> <option value="18">adsfasdf</option> </select> This is the viewsource after my click event.I don't know why it sets the value of the select option to 17 instead of selecting the 17th option <select name=dr_staff" onchange="__doPostBack('pc_staff','')" language="javascript" id="dr_staff"> <option selected="selected" value="17">Select</option> <option value="16">Joe,G</option> <option value="17">Martin,L</option> <option value="18">adsfasdf</option> </select> Quote
Arch4ngel Posted May 10, 2004 Posted May 10, 2004 You'll have to show us more code... and maybe the part where you add the element "Select". Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
srithil Posted May 10, 2004 Author Posted May 10, 2004 I am populating the dropdownlist from my table Nt_st_staff in my pageload event The design of the table is st_Staff_id St_Staff_name 15 Select 16 Joe,G 17 Martin,L 18 adsfasdf thanks Quote
srithil Posted May 10, 2004 Author Posted May 10, 2004 hai , my problem is solved using the following code with selectedindex. thanks. dr_staff.SelectedIndex=pp_staff.Items.IndexOf(dr_staff.Items.FindByValue(mydr1("dr_staff"))) Quote
fischerj Posted May 12, 2004 Posted May 12, 2004 Try dr_staff.SelectedValue=mydr("st_staff") Best Regards, Jay I have a dropdownlist staff. I try to select the option by its value property. I get the value from the datareader correctly,but when i use the following code dr_staff.SelectedItem.value=mydr("st_staff") only the first option is selected.The correct option is not selected. anybody pls help. tx Quote
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.