Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does the dropdownlist always select the same item ?

Status
Not open for further replies.

chu2654

Programmer
Sep 16, 2005
5
TW
Hello, I use a drop downlist "txtmajor" to retrieve data from sql server.

txtmajor.Items.clear()
While reader.Read()
txtmajor.Items.Add(reader.Item("No") & "/" & reader.Item("Subject"))
End While

But when I use txtmajor.SelectedItem.Text.ToString to show the selected item, it is always the first item. How can I solve the problem ?
 
At what point are you loading the values into the drop down list?

Also, you can simplify your code by binding the reader to the list like so

Code:
txtmajor.DataSource = reader;
txtmajor.DataValueField = "id";
txtmajor.DataTextField = "Text To Display";
txtmajor.DataBind();

And you can access the selected value with

Code:
string selectedValue = txtmajor.SelectedValue;
 
I set the dropdownlist in page_load. Your method still doesn't work.

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
dim username as string
Call Show_Subject()

End Sub

Sub Show_Subject()
Dim SelectCmd As String
Dim reader As sqlDataReader
SelectCmd = "select no,name from class"
reader = Create_Rd("Diglearning", SelectCmd)
txtmajor.DataSource = reader
txtmajor.DataValueField = "no"
txtmajor.DataTextField = "no"
txtmajor.DataBind()

End Sub

Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
response.write(txtmajor.SelectedValue)
End Sub

<html>
<head><title></title></head>
<body background="bg1.gif"><center>
<form runat="server">


<asp:DropDownList id="txtmajor" runat="server">
<asp:ListItem>Class</asp:ListItem>
</asp:DropDownList></td>
</tr>





<tr>
<td colspan="2" bgColor=#FFCC66 width="376">
<asp:Button id="btnSend" runat="server" Text="Send" onClick="btnSend_Click"/></td>
</tr>
</table>
<asp:Label id="lblShow1" BackColor="Yellow" runat="server"/>

</asp:panel>
<asp:panel id="panel2" runat="server" width="300" backColor="#CCFFCC">
<asp:Label id="lblShow" runat="server"/>
</asp:panel>
</form></center>
</P>

</body></html>
 
You are populating the list with each postback

You need to do something like below in the Page_Load. It may not be syntactically perfect, as I am not a VB programmer, but the basic idea is right.

Code:
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
 dim username as string

 if(IsPostBack = false)
  Call Show_Subject()
 End If
    
 End Sub
 
You also don't need to use the "Call" method in .NET


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
If you want to reload the list every time, use a session variable to capture your selection on the post back. In c# for instance:

if(Page.IsPostBack &&
DropDownList1.SelectedIndex != -1 &&
DropDownList1.SelectedIndex != 0) {
Session["VARIABLE"] = DropDownList1.SelectedValue;
}
DropDownList1.Items.Clear();
//ADD WHATEVER ITEMS YOU WANT TO THE LIST HERE

if(Session["VARIABLE"] != null) {
DropDownList1.SelectedValue = Session["VARIABLE"].ToString().Trim();
} else
DropDownList1.SelectedIndex = 0;

That should work for you if you can translate the concept back to VB....
 
my 2 pennies, i think its your DataValueField, and DataTextField no populating right...

txtmajor.DataValueField = "no"
txtmajor.DataTextField = "no"

lauch the page with your DDL, then right click the page, view source - scroll to your dropdown list, and it will render as a <SELECT> with <OPTION value="">Bla</OPTION>s

id say post that here, but dont, just make sure each value is unique, the texts can be dup'd though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top