Adding additional properties to list items in a dropDownlist through inheritance

Discussion in 'ASP .Net' started by RSH, Jan 2, 2007.

  1. RSH

    RSH Guest

    Hi,

    I have a situation where I need to add several "Hidden" properties to list
    items in a dropdownlist. By default the DropDownList item has two
    properties with regards to the listitems collection, Text and Value. I need
    to add a DivisionID, and DepartmentID.

    I assumed i could simply override the ListItem class and add the additional
    properties. is this in fact the case? If so what would the code look like?

    Thanks in advance for any information you can provide.

    Ron
     
    RSH, Jan 2, 2007
    #1
    1. Advertising

  2. RSH

    Mark Rae Guest

    "RSH" <> wrote in message
    news:...

    > I have a situation where I need to add several "Hidden" properties to list
    > items in a dropdownlist. By default the DropDownList item has two
    > properties with regards to the listitems collection, Text and Value.


    That's right.

    > I need to add a DivisionID, and DepartmentID.


    OK.

    > I assumed i could simply override the ListItem class and add the
    > additional properties. is this in fact the case? If so what would the
    > code look like?


    Run your web app and navigate to the page with the DropDownList - do a View
    Source. The DropDownList webcontrol simply creates the <select> HTML tag,
    and the various <option> elements in it. Whereas you could override the
    ListItem class in the way you describe by adding additional properties,
    ASP.NET would simply ignore them when creating the HTML markup to stream
    down to the client.

    > Thanks in advance for any information you can provide.


    Fortunately, this is quite easy to work around.

    <asp:DropDownList ID="cmbTest" runat="server" />
    <asp:Button ID="cmdTest" runat="server" Text="Test" OnClick=cmdTest_Click />

    protected void Page_Load(object sender, EventArgs e)
    {
    ListItem objItem = null;

    objItem = new ListItem();
    objItem.Value = "1¬2¬3";
    objItem.Text = "One";
    cmbTest.Items.Add(objItem);

    objItem = new ListItem();
    objItem.Value = "4¬5¬6";
    objItem.Text = "Two";
    cmbTest.Items.Add(objItem);
    }

    protected void cmdTest_Click(object sender, EventArgs e)
    {
    string strValue = cmbTest.SelectedValue.Split('¬')[0];
    string strDivision = cmbTest.SelectedValue.Split('¬')[1];
    string strDepartment = cmbTest.SelectedValue.Split('¬')[2];
    }
     
    Mark Rae, Jan 2, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Henry
    Replies:
    5
    Views:
    10,043
    Steven Cheng[MSFT]
    Aug 3, 2004
  2. estafford
    Replies:
    2
    Views:
    2,733
    estafford
    Nov 18, 2004
  3. Bart Van Hemelen
    Replies:
    2
    Views:
    4,678
    Bart Van Hemelen
    Sep 4, 2006
  4. Anjan Bhowmik
    Replies:
    1
    Views:
    477
    Misbah Arefin
    Feb 14, 2008
  5. AAaron123
    Replies:
    2
    Views:
    425
    AAaron123
    Oct 7, 2009
Loading...

Share This Page