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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting dropdownlist value changed in client side seen in code behind

Status
Not open for further replies.

zma

Programmer
May 30, 2006
25
US
Hello,
DropDownList2's item values are assigned on the client side with javascript. THis is so they can depend on the item values of DropDownList1 without involving the server. WHen a button is clicked to submit the form, a query is run in the code behind using values from DropDownList2 and I get this error

System.NullReferenceException: Object reference not set to an instance of an object.

THis code behind line is highlighted in the error message

Dim wherepart As String = "where " + DropDownList2.SelectedItem.Value + " like @lookfor" + " and "

I think DropDownList2.SelectedItem.Value is not being seen there.

When DropDownList2's item values are set in a control in the aspx page or assigned in the code behind, the query runs fine.

1. HOw do you get a value assigned client side to been seen in a function in the code behind?
2. Should I not do things this way at all?
 
The code behind code does not work because a postback has not happend, so the selecteditem does not exist server side. You will have to handle this client side. When the user selects an item, then save the value to a hidden field(set runat="server"). Then you can use the hidden field's value in the code behind.
 
Have you tried the AJAX cascading dropdown control? its pretty cool doing what you want. Anyway, while i was using it, i had a somewhat related issue to yours.

DropDownList.SelectedIndex was never set, so when postback happened, it was always 0, so i looked around and was able to test the DropDownList.SelectedValue. This was a string equal to "" (nothing), and if it had something ("3") i would update my values.

So instead of "SelectedItem.Value", try DropDownList2.SelectedValue, might work, yet not the root solution.

Jbensons idea could work too, but even an <asp:DropDownList is rendered as a plain ol select, so either approach could work, as long as you have your Select with a runat=server, it will maintain its Value on postback, and you wouldnt need to use a hidden input.

As per my issue, i think i should have been checking the CCD control not the dropdown itself, but our net has been down for a week and havent been able to research it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top