7 Replies - 368 Views - Last Post: 09 May 2020 - 12:20 PM Rate Topic: -----

#1 DarenR   User is offline

  • D.I.C Lover

Reputation: 713
  • View blog
  • Posts: 4,593
  • Joined: 12-January 10

selected value is null?

Posted 08 May 2020 - 11:45 AM

so i have a list box which is filled as follows:

   lbxEscaltionsCourses.DataTextField = "FacultyName";
                lbxEscaltionsCourses.DataValueField = "SectionID";  
                lbxEscaltionsCourses.DataSource = list;
                lbxEscaltionsCourses.DataBind();
                


it loads the list box and i can see the items

i am able to click on said item and it highlights it
when i do a submit call which is in its own method as follows:

 protected void Save()
        {

var selectedItem = lbxEscaltionsCourses.SelectedItem;
            string courseId = null;


            if (lbxEscaltionsCourses.SelectedIndex > -1)
            {

                courseId = lbxEscaltionsCourses.SelectedItem.ToString();
            }
}



the coursid is null even though when you hover over the databind it is all there and it shows on page.
the selected index also shows -1 even when i select the bottom item.

not sure why am i missing something that my tired eyes cant see?

Is This A Good Question/Topic? 0
  • +

Replies To: selected value is null?

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15806
  • View blog
  • Posts: 63,313
  • Joined: 12-June 08

Re: selected value is null?

Posted 08 May 2020 - 12:07 PM

When are you loading the data for the list? Chuck it in a 'not is postback' else you reload it before your button event fires to get the value.

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //load data here
            }
        }

Was This Post Helpful? 1
  • +
  • -

#3 DarenR   User is offline

  • D.I.C Lover

Reputation: 713
  • View blog
  • Posts: 4,593
  • Joined: 12-January 10

Re: selected value is null?

Posted 08 May 2020 - 12:11 PM

life saver--- stared at this too long
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15806
  • View blog
  • Posts: 63,313
  • Joined: 12-June 08

Re: selected value is null?

Posted 08 May 2020 - 12:14 PM

That postback is always an unexpected kick in the nuts when least wanted.

Posted Image
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7463
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: selected value is null?

Posted 08 May 2020 - 05:23 PM

Hopefully by now your are starting to grasp why WebForms was abandoned by most sane web developers. It tries to apply the stateful Win32 API paradigm to the stateless web, and things don't quite work without actually understanding that the web is stateless.

Save your sanity, move on to ASP.NET MVC, or do what the cool kids do: use ASP.NET WebAPI on the server, and Angular or React on the client.
Was This Post Helpful? 0
  • +
  • -

#6 DarenR   User is offline

  • D.I.C Lover

Reputation: 713
  • View blog
  • Posts: 4,593
  • Joined: 12-January 10

Re: selected value is null?

Posted 09 May 2020 - 09:32 AM

yeah i dont have a choice on what i am using-- they are actually going away from mvc and back to vanilla asp.net -- hell they even gave me vs2012 to use instead of the latest-- so needless to say they are a little bit behind the times
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7463
  • View blog
  • Posts: 25,113
  • Joined: 05-May 12

Re: selected value is null?

Posted 09 May 2020 - 09:45 AM

View PostDarenR, on 09 May 2020 - 12:32 PM, said:

they even gave me vs2012 to use instead of the latest

And how did they exactly pay for that license considering that version is out of support? Or is this a case of one does not look a gift horse in the mouth?
Was This Post Helpful? 0
  • +
  • -

#8 DarenR   User is offline

  • D.I.C Lover

Reputation: 713
  • View blog
  • Posts: 4,593
  • Joined: 12-January 10

Re: selected value is null?

Posted 09 May 2020 - 12:20 PM

they buy licenses and store them -- ie group license
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1