listbox.selectedvalue

Posts   
 
    
e106199
User
Posts: 175
Joined: 09-Sep-2006
# Posted on: 09-Sep-2006 04:54:38   

hi, i have a listbox that i fill with an entity collection. the datatextfield is the Fullname and the datavaluefield is the id number. i do the databinding in page load event.

this is just fine. then i try to catch the listbox1.selectedvalue and assing it to an integer var but i get the type format error. somehow the listbox1.selectedvalue is always "". what am i doing wrong? thanks -shane

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 09-Sep-2006 10:10:19   

please specify .NET version, llblgen pro version and if possible a little codesnippet to illustrate what exactly you're doing. The HTML looks ok? (so there are options to select with proper values? (check the html source at runtime)

Frans Bouma | Lead developer LLBLGen Pro
e106199
User
Posts: 175
Joined: 09-Sep-2006
# Posted on: 10-Sep-2006 03:50:54   

hi, .net 2.0 c# llblgen pro 2 the html looks good, the text and value shows ok for the listbox.

    userCollection allUsers = new userCollection();
    allUsers.GetMulti(null);
    ListBox1.DataSource = allUsers;
    ListBox1.DataTextField = "FullName";
    ListBox1.DataValueField = "UserId";
    ListBox1.DataBind();

this is wat i got in my page load event.

i have a button, when its clicked i m trying to collect Listbox1.SelectedValue and all i get is "".

why cant i get the id number of the selected user? thanks

Jez
User
Posts: 198
Joined: 01-May-2006
# Posted on: 10-Sep-2006 13:37:15   

Is your code wrapped in an if(!IsPostBack) ?

If not, then the code will execute on every page load (including postbacks) and will re-bind the listbox. When a control is databound, it will lose its selectedvalue.

If you're not doing the following, then this may fix your problem:


protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        userCollection allUsers = new userCollection();
        allUsers.GetMulti(null);
        ListBox1.DataSource = allUsers;
        ListBox1.DataTextField = "FullName";
        ListBox1.DataValueField = "UserId";
        ListBox1.DataBind();
    }
}

e106199
User
Posts: 175
Joined: 09-Sep-2006
# Posted on: 10-Sep-2006 18:25:17   

long live Jez, i cant believe i missed that point. i thought since databind is done throug llblgen, maybe it lost its selected value. thanks a lot