8 Replies - 388 Views - Last Post: 13 May 2020 - 10:28 AM Rate Topic: -----

#1 DarenR   User is offline

  • D.I.C Lover

Reputation: 708
  • View blog
  • Posts: 4,580
  • Joined: 12-January 10

listbox doesnt allow tag property?

Posted 12 May 2020 - 10:07 AM

I have a list box which i need to add a tag property for an id i need to retrieve for a selected item

currently i have
    myListBox.DataTextField = "fname";
                myListBox.DataValueField = "sID";  
                <--here i was trying to add myListBox.Tag = "anotherID" -->
                myListBox.DataSource = list;
                myListBox.DataBind();



there are 2 id's i need to get from the selected item in the list box of which go to 2 different things

is there a reason the Tag option is not available for the listbox or is there a better way to add the 2nd id associated with the lines however it can not be visible on the text side

so for example

currently it displays as follows


bio-123 -- biology -- mr smith

in the actual value field we have the course ID



what i need it to do is
bio-123 -- biology -- mr smith
in the actual value field we need the course ID and the professor id


Is This A Good Question/Topic? 0
  • +

Replies To: listbox doesnt allow tag property?

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15799
  • View blog
  • Posts: 63,297
  • Joined: 12-June 08

Re: listbox doesnt allow tag property?

Posted 12 May 2020 - 11:29 AM

Nope, no tag directly, but you can add an attribute called 'tag' (or what ever) and add a value.
   ListBox1.Attributes.Add("tag", "123");


        protected void Button1_Click(object sender, EventArgs e)
        {
          var aa =  ListBox1.Attributes["tag"];
        }

.. or a hidden field.
Was This Post Helpful? 0
  • +
  • -

#3 DarenR   User is offline

  • D.I.C Lover

Reputation: 708
  • View blog
  • Posts: 4,580
  • Joined: 12-January 10

Re: listbox doesnt allow tag property?

Posted 12 May 2020 - 12:03 PM

unfortunately that doesnt do what i need. It added the tag attribute to the list box but not to the options.
also it doesnt recognize the tag field to fill in the required info.

the way they do things here is that they load a list with a web api then they take that list and make the listbox datasoource= list
it has the datatextfield and the datavaluefield options
so when i added the following:
myListBox.Attributes.Add("tag", "FatID");

it added it to the main list and not to the options that are dynamically created from the datasource
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7454
  • View blog
  • Posts: 25,095
  • Joined: 05-May 12

Re: listbox doesnt allow tag property?

Posted 12 May 2020 - 01:17 PM

The Tag property is a WinForms thing. It's the WinForms equivalent of the Win32 API: GWL_USER. As I recall, your past few questions have been about WebForms, rather than WinForms. One of the many things the WinForms to WebForms interpretation/porting lost was the Tag property since there was no readily available HTML equivalent.

View PostDarenR, on 12 May 2020 - 03:03 PM, said:

unfortunately that doesnt do what i need. It added the tag attribute to the list box but not to the options.

But that's exactly what you were asking for:

View PostDarenR, on 12 May 2020 - 01:07 PM, said:

I have a list box which i need to add a tag property for an id i need to retrieve for a selected item

currently i have
    myListBox.DataTextField = "fname";
                myListBox.DataValueField = "sID";  
                <--here i was trying to add myListBox.Tag = "anotherID" -->
                myListBox.DataSource = list;
                myListBox.DataBind();


Only now is it clearer that you are trying to add the tag to each listbox item, rather than to the listbox itself.
Was This Post Helpful? 0
  • +
  • -

#5 DarenR   User is offline

  • D.I.C Lover

Reputation: 708
  • View blog
  • Posts: 4,580
  • Joined: 12-January 10

Re: listbox doesnt allow tag property?

Posted 12 May 2020 - 04:25 PM

that list box is dynamically populated
and as for the question:

Quote

there are 2 id's i need to get from the selected item in the list box of which go to 2 different things

is there a reason the Tag option is not available for the listbox or is there a better way to add the 2nd id associated with the lines however it can not be visible on the text side

This post has been edited by DarenR: 12 May 2020 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7454
  • View blog
  • Posts: 25,095
  • Joined: 05-May 12

Re: listbox doesnt allow tag property?

Posted 12 May 2020 - 04:53 PM

The classic way HTML is done to have a display string and a value string. If you need to store two or more values, then you apply the poor practice of being "stringly-typed" -- meaning that you put the two or more values into a single string. Later when a selection is made, you take that value string and parse it to get the two values back out. This was also the time before JSON was popular, so the typical web developer would use a comma or semicolon separated string as the value. When it was time to parse, then you just to a call to split(). The more daring would store a substring of Javascript and call eval(). (In modern web sites do not do this. eval() is root of cross site scripting evil vulnerabilities.) Nowadays, you can stick in some JSON as that value string.
Was This Post Helpful? 0
  • +
  • -

#7 DarenR   User is offline

  • D.I.C Lover

Reputation: 708
  • View blog
  • Posts: 4,580
  • Joined: 12-January 10

Re: listbox doesnt allow tag property?

Posted 13 May 2020 - 05:18 AM

View PostSkydiver, on 12 May 2020 - 07:53 PM, said:

The classic way HTML is done to have a display string and a value string. If you need to store two or more values, then you apply the poor practice of being "stringly-typed"



dude if you saw the db you would pull your hair out

a "professional" database expert made this years ago and all the primary keys are varchar instead of integer

so we have tables like 00001 as a varchar
another fun thing is that they dont keep the same naming conventions on the table variables
for example:
tbl_1   studentID primary
tbl_2   imAStudent  foreign key
tbl_3   x_student_x  foreign key
tbl_4   student        foreign key



and all those keys above are StudentID relating to the same column
Was This Post Helpful? 0
  • +
  • -

#8 DarenR   User is offline

  • D.I.C Lover

Reputation: 708
  • View blog
  • Posts: 4,580
  • Joined: 12-January 10

Re: listbox doesnt allow tag property?

Posted 13 May 2020 - 06:02 AM

i said screw it and created a sql query that can take the id that i do have and do a search for what i need-- and i created a method to grab the info
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7454
  • View blog
  • Posts: 25,095
  • Joined: 05-May 12

Re: listbox doesnt allow tag property?

Posted 13 May 2020 - 10:28 AM

Sounds like that "professional" database expert became a pro during the .COM bubble when they were hiring anyone who could turn on a computer.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1