problem with dropdownlist selection

srithil

Newcomer
Joined
Apr 30, 2004
Messages
21
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
 
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.
 
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.
 
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>
 
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.
 
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
 
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.
 
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>
 
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
 
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")))
 
Try dr_staff.SelectedValue=mydr("st_staff")

Best Regards,

Jay

srithil said:
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
 
Back
Top