can't get asp.net ListBox SelectedValue

SweatCoder
SweatCoder used Ask the Experts™
on
No matter what I try, I can't get the SelectedValue from my listbox on postback. I have triple-checked and even verified by stepping through code that I'm not rebinding my listbox to the datasource on postback (button click).

listbox declaration:
<asp:ListBox ID="listAvailableAMCs" Rows="8" Width="300px" DataValueField="ID" DataTextField="Company" runat="server"></asp:ListBox>

Listbox data population works fine.

A button click calls this:

protected void LinkAMC(object sender, EventArgs e)
    {
        if(listAvailableAMCs.SelectedValue != string.Empty)
            Utilities.LinkAMC(int.Parse(listAvailableAMCs.SelectedValue));

        LoadGridData();
    }

SelectedValue is always blank. SelectedText is blank and SelectedIndex is -1.

A view source reveals this html:

<select size="8" name="ctl00$MasterMain$listAvailableAMCs" id="MasterMain_listAvailableAMCs" style="width:300px;">
      <option value="19">Como AMC</option>
      <option value="21">Sinatra AMC</option>
      <option value="20">Martin AMC</option>
</select>

Those option values and text values are exactly right.

What am I missing?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010
Commented:
The code seems to be fine, Check the following points:
1. You are not reloading the listbox after each postback
2. Make sure that you do not have ViewStateEnabled="false" for a parent container
3. You are rebinding the ListBox each time, instead of not IsPostback

Author

Commented:
1 is good, as mentioned in original post.
2 - viewstate is disabled for the whole site because I don't need. Maybe you've got a point here, but I use asp:DropDownList on other pages and it works fine with viewstate turned off. Why would ListBox be different from ddl in that respect?
3- Not sure exactly what you mean. If i'm not in a postback, that's when I bind the listbox.
Please post your Page_Load Event Code.. I think You are Missing the IsPostBack checking in the load event.. I need to modify your Page_Load event so that you get ur SelectedValue right when you click the button..
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Else please post the complete Code behind Code so that I can help you out much better

Author

Commented:
I am only binding data source when not in a postback:

if (!IsPostBack)
        {
            listAvailableAMCs.DataSource = Utilities.GetLinkedAMCs(_LenderBrokerID);
            listAvailableAMCs.DataBind();
        }

I have noticed that everything works fine when I use a declarative data source in the aspx, but this becomes awkward. I'd rather get my data from a class library where I can do some logic checks, pass in variables, parameterize the query, etc.
I had to implement a javascript approach to this because I couldn't get asp.net to work right. I'm sure there's a correct way to solve this, but I have it working fine and I don't want to go back and mess with it again.

Author

Commented:
Had to use javascript, asp.net solution never worked. Awarding points just so I can close this.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial