<asp:DropDownList ID="customerInput" runat="server" DataSourceID="SqlDataSource1"
DataTextField="name" DataValueField="customerId" >
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connection %>"
SelectCommand="SELECT [name], [customerId] FROM [Customers] ORDER BY [name]">
</asp:SqlDataSource>
I'm trying to set the selected value from a db in the page load, but it's just staying on the first entry. Any ideas why? Here's my code behind
'make sure this isn't a postback
If Not Page.IsPostBack Then
customerInput.DataBind()
'populate everything
Dim sql = "SELECT ticketDate, name, Tickets.address, description, priority, foremanId, customerFk FROM Tickets INNER JOIN Customers ON " & _
" customerId = customerFk WHERE ticketId = " & Request.QueryString("tId")
Using myconn As New SqlConnection(conn)
myconn.Open()
Dim cmd = New SqlCommand(sql, myconn)
Dim reader = cmd.ExecuteReader
While reader.Read()
customerInput.Text = reader("name")
customerInput.SelectedValue = reader("customerFk")
...
Any ideas why? I get no error. I've verified that customerFk is a good value that does exist in the dd.
Why do you have a while loop? You should just be getting one row, right? Anyway if that doesn change anything, I suggest you handle the DataBound event if the DDL and try Setting the SelectedValue property there.
What this does is ensure that there is actually an item with the value, and if there is it clears out all selection and it then finds it and sets the value to true. You may not need the ClearSelection call as it may fire the selectedindexchanged event so
you'll want to watch for that.
Also, you should always test to ensure that not only did the reader get a row, but that the individual columns returned are not null.
Don't forget to mark useful responses as Answer if they helped you towards a solution.
That doesn't work. Here's my problem, I know that the value exists, I can see it if I load the page and inspect the dropdown. However, when I try to set that value it just doesn't work. Here's what I'm using.
Are you sure your dropdown's been databound by the time you set SelectedValue? Setting SelectedValue doesn't do anything if the dropdown doesn't have any items in it at the time.
That was my first thought before I posted on here. I have a data source in my aspx, but I call the databind before I set the value, would this still not bind it properly?
'make sure this isn't a postback
If Not Page.IsPostBack Then
customerInput.DataBind()
'populate everything
Dim sql = "SELECT ticketDate, estimateDate, startDate, name, Tickets.address, description, priority, foremanId, customerFk FROM Tickets INNER JOIN Customers ON " & _
" customerId = customerFk WHERE ticketId = " & Request.QueryString("tId")
Using myconn As New SqlConnection(conn)
myconn.Open()
Dim cmd = New SqlCommand(sql, myconn)
Dim reader = cmd.ExecuteReader
While reader.Read()
customerInput.Text = reader("name")
customerInput.ClearSelection()
customerInput.SelectedValue = reader("customerFk")
'customerInput.SelectedIndex = customerInput.Items.IndexOf(customerInput.Items.FindByValue(reader("customerFk")))
That's odd. I just tried out a similar slice of code and can reproduce your problem. I'm honestly at a loss, though. I've been digging around the DropDownList/ListControl source code to try and find an answer, but I've only come up with more questions regarding
how the .NET web controls work.
So I have a question for you. When you set the SelectedValue, if you then check the SelectedValue immediately afterwards, is it set to what you'd expect it to be, or is it just an empty string? The reason I ask, is cause I noticed in the ListControl's SelectedValue
property, the setter will not throw an exception if the value doesn't exist(unless the page is a post back, or the non-existant value is set before databinding occurs).
And as an aside...
You should really use parameterized queries. Someone could theoretically go to your page with the url "http://www.site.com/page.aspx?tId=4; DROP ALL TABLES" and delete your database. If you're not familiar with using parameters, they're easy!
Dim sql = "select * from MyTable where ID = @idParameter"
cmd.Parameters.AddWithValue("idParameter", 42)
mandrews1234
Member
335 Points
579 Posts
Can't set selected value of dropdown
Feb 27, 2012 06:06 PM|LINK
I have a dropdown like so
<asp:DropDownList ID="customerInput" runat="server" DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="customerId" > </asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connection %>" SelectCommand="SELECT [name], [customerId] FROM [Customers] ORDER BY [name]"> </asp:SqlDataSource>I'm trying to set the selected value from a db in the page load, but it's just staying on the first entry. Any ideas why? Here's my code behind
'make sure this isn't a postback If Not Page.IsPostBack Then customerInput.DataBind() 'populate everything Dim sql = "SELECT ticketDate, name, Tickets.address, description, priority, foremanId, customerFk FROM Tickets INNER JOIN Customers ON " & _ " customerId = customerFk WHERE ticketId = " & Request.QueryString("tId") Using myconn As New SqlConnection(conn) myconn.Open() Dim cmd = New SqlCommand(sql, myconn) Dim reader = cmd.ExecuteReader While reader.Read() customerInput.Text = reader("name") customerInput.SelectedValue = reader("customerFk")...
Any ideas why? I get no error. I've verified that customerFk is a good value that does exist in the dd.
MetalAsp.Net
All-Star
112162 Points
18252 Posts
Moderator
Re: Can't set selected value of dropdown
Feb 27, 2012 06:11 PM|LINK
markfitzme
Star
14433 Points
2227 Posts
Re: Can't set selected value of dropdown
Feb 27, 2012 06:20 PM|LINK
You can't set the selectedvalue, it's read only. It's used to get the value of the selected item.
My example is in C# but should be easily convertible to VB.
if(customerInput.Items.FindByValue(reader("customerFk").ToString() != null))
{
customerInput.ClearSelection();
customerInput.Items.FindByValue(reader("customerFk").Selected = true;
}
What this does is ensure that there is actually an item with the value, and if there is it clears out all selection and it then finds it and sets the value to true. You may not need the ClearSelection call as it may fire the selectedindexchanged event so you'll want to watch for that.
Also, you should always test to ensure that not only did the reader get a row, but that the individual columns returned are not null.
mandrews1234
Member
335 Points
579 Posts
Re: Can't set selected value of dropdown
Feb 28, 2012 09:55 PM|LINK
That doesn't work. Here's my problem, I know that the value exists, I can see it if I load the page and inspect the dropdown. However, when I try to set that value it just doesn't work. Here's what I'm using.
customerInput.ClearSelection()
customerInput.SelectedValue = reader("customerFk")
Response.Write(reader("customerFk"))
the write outputs 86 which I do see in the dropdown as a value. however the selected item is just the very first one.
rossisdead2
Participant
1313 Points
300 Posts
Re: Can't set selected value of dropdown
Feb 28, 2012 10:06 PM|LINK
Are you sure your dropdown's been databound by the time you set SelectedValue? Setting SelectedValue doesn't do anything if the dropdown doesn't have any items in it at the time.
Cheng Bao
Member
191 Points
126 Posts
Re: Can't set selected value of dropdown
Feb 28, 2012 10:16 PM|LINK
Yes you can. Altough if the SelectedValue is not one of values of the ListItems, an exception will be thrown.
mandrews1234
Member
335 Points
579 Posts
Re: Can't set selected value of dropdown
Feb 28, 2012 10:37 PM|LINK
That was my first thought before I posted on here. I have a data source in my aspx, but I call the databind before I set the value, would this still not bind it properly?
'make sure this isn't a postback If Not Page.IsPostBack Then customerInput.DataBind() 'populate everything Dim sql = "SELECT ticketDate, estimateDate, startDate, name, Tickets.address, description, priority, foremanId, customerFk FROM Tickets INNER JOIN Customers ON " & _ " customerId = customerFk WHERE ticketId = " & Request.QueryString("tId") Using myconn As New SqlConnection(conn) myconn.Open() Dim cmd = New SqlCommand(sql, myconn) Dim reader = cmd.ExecuteReader While reader.Read() customerInput.Text = reader("name") customerInput.ClearSelection() customerInput.SelectedValue = reader("customerFk") 'customerInput.SelectedIndex = customerInput.Items.IndexOf(customerInput.Items.FindByValue(reader("customerFk")))rossisdead2
Participant
1313 Points
300 Posts
Re: Can't set selected value of dropdown
Feb 29, 2012 12:07 AM|LINK
That's odd. I just tried out a similar slice of code and can reproduce your problem. I'm honestly at a loss, though. I've been digging around the DropDownList/ListControl source code to try and find an answer, but I've only come up with more questions regarding how the .NET web controls work.
So I have a question for you. When you set the SelectedValue, if you then check the SelectedValue immediately afterwards, is it set to what you'd expect it to be, or is it just an empty string? The reason I ask, is cause I noticed in the ListControl's SelectedValue property, the setter will not throw an exception if the value doesn't exist(unless the page is a post back, or the non-existant value is set before databinding occurs).
And as an aside...
You should really use parameterized queries. Someone could theoretically go to your page with the url "http://www.site.com/page.aspx?tId=4; DROP ALL TABLES" and delete your database. If you're not familiar with using parameters, they're easy!
Dim sql = "select * from MyTable where ID = @idParameter" cmd.Parameters.AddWithValue("idParameter", 42)MetalAsp.Net
All-Star
112162 Points
18252 Posts
Moderator
Re: Can't set selected value of dropdown
Feb 29, 2012 12:23 AM|LINK