Displaying dictionary collection key/value pairs formatted with html markup on a page

Discussion in 'ASP .Net' started by Andy B, Apr 22, 2008.

  1. Andy B

    Andy B Guest

    I have the object property StockContract.Dictionary which is a dictionary
    collection of <string, string> key/value pairs. I need to be able to
    retreive the keys and their values and display them on a page. I want to use
    something like a repeater or something like that. I don't know if this would
    be code I will have to manually write myself, or if it can be done with
    dataBinding of some sort. I am using vs2008 and c# 3.5. Any ideas on how to
    do something like this?
    Andy B, Apr 22, 2008
    #1
    1. Advertising

  2. Andy B

    Andy B Guest

    I tried to put the StockContract.Dictionary property as a
    listView.DataSource. The compiler isn't complaining about it yet, but there
    is another question about this kind of databinding. Inside the item
    template, What do I use for the Eval() method to get the values out of the
    Dictionary collection? I tried Eval("Keys") as the text for a Label control,
    but that is what I litterally end up with as the text instead of the text of
    the key. Any ideas how to deal with something like this?


    "Mark Rae [MVP]" <> wrote in message
    news:%...
    > "Andy B" <> wrote in message
    > news:...
    >
    >>I have the object property StockContract.Dictionary which is a dictionary
    >>collection of <string, string> key/value pairs. I need to be able to
    >>retreive the keys and their values and display them on a page. I want to
    >>use something like a repeater or something like that. I don't know if this
    >>would be code I will have to manually write myself, or if it can be done
    >>with dataBinding of some sort. I am using vs2008 and c# 3.5. Any ideas on
    >>how to do something like this?

    >
    > <asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="true" />
    >
    > Dictionary<string, string> dicTest = new Dictionary<string, string>();
    > dicTest.Add("1st", "First");
    > dicTest.Add("2nd", "Second");
    > dicTest.Add("3rd", "Third");
    > gvTest.DataSource = dicTest;
    > gvTest.DataBind();
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    Andy B, Apr 23, 2008
    #2
    1. Advertising

  3. Andy B

    Andy B Guest

    I am trying to databind to the collection like you normally would say to a
    database table. I need to cycle through all of the keys and print out their
    key text and value text on a page. The print out has to be formatted with
    html markup...
    "Mark Rae [MVP]" <> wrote in message
    news:O%...
    > "Andy B" <> wrote in message
    > news:...
    >
    > [top-posting corrected]
    >
    >>>>I have the object property StockContract.Dictionary which is a
    >>>>dictionary collection of <string, string> key/value pairs. I need to be
    >>>>able to retreive the keys and their values and display them on a page. I
    >>>>want to use something like a repeater or something like that. I don't
    >>>>know if this would be code I will have to manually write myself, or if
    >>>>it can be done with dataBinding of some sort. I am using vs2008 and c#
    >>>>3.5. Any ideas on how to do something like this?
    >>>
    >>> <asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="true" />
    >>>
    >>> Dictionary<string, string> dicTest = new Dictionary<string, string>();
    >>> dicTest.Add("1st", "First");
    >>> dicTest.Add("2nd", "Second");
    >>> dicTest.Add("3rd", "Third");
    >>> gvTest.DataSource = dicTest;
    >>> gvTest.DataBind();

    >>
    >> I tried to put the StockContract.Dictionary property as a
    >> listView.DataSource. The compiler isn't complaining about it yet, but
    >> there is another question about this kind of databinding. Inside the item
    >> template, What do I use for the Eval() method to get the values out of
    >> the Dictionary collection? I tried Eval("Keys") as the text for a Label
    >> control, but that is what I litterally end up with as the text instead of
    >> the text of the key. Any ideas how to deal with something like this?

    >
    > Not sure what you're trying to do exactly...
    >
    > What you've got here is, essentially, a two-dimensional array - which
    > field from which row are you trying to use as the text in a Label
    > control...?
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    Andy B, Apr 23, 2008
    #3
  4. Andy B

    Andy B Guest

    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "Andy B" <> wrote in message
    > news:%...
    >
    > [top-posting corrected again]
    >
    >>>>>>I have the object property StockContract.Dictionary which is a
    >>>>>>dictionary collection of <string, string> key/value pairs. I need to
    >>>>>>be able to retreive the keys and their values and display them on a
    >>>>>>page. I want to use something like a repeater or something like that.
    >>>>>>I don't know if this would be code I will have to manually write
    >>>>>>myself, or if it can be done with dataBinding of some sort. I am using
    >>>>>>vs2008 and c# 3.5. Any ideas on how to do something like this?
    >>>>>
    >>>>> <asp:GridView ID="gvTest" runat="server" AutoGenerateColumns="true" />
    >>>>>
    >>>>> Dictionary<string, string> dicTest = new Dictionary<string, string>();
    >>>>> dicTest.Add("1st", "First");
    >>>>> dicTest.Add("2nd", "Second");
    >>>>> dicTest.Add("3rd", "Third");
    >>>>> gvTest.DataSource = dicTest;
    >>>>> gvTest.DataBind();
    >>>>
    >>>> I tried to put the StockContract.Dictionary property as a
    >>>> listView.DataSource. The compiler isn't complaining about it yet, but
    >>>> there is another question about this kind of databinding. Inside the
    >>>> item template, What do I use for the Eval() method to get the values
    >>>> out of the Dictionary collection? I tried Eval("Keys") as the text for
    >>>> a Label control, but that is what I litterally end up with as the text
    >>>> instead of the text of the key. Any ideas how to deal with something
    >>>> like this?
    >>>
    >>> Not sure what you're trying to do exactly...
    >>>
    >>> What you've got here is, essentially, a two-dimensional array - which
    >>> field from which row are you trying to use as the text in a Label
    >>> control...?

    >>
    >>I am trying to databind to the collection like you normally would say to a
    >>database table.

    >
    > Can you clarify what you mean by that, please? You don't databind *to* a
    > database table - you databind *from* a database table *to* a webcontrol
    > such as a GridView etc...
    >
    >> I need to cycle through all of the keys and print out their key text and
    >> value text on a page. The print out has to be formatted with html
    >> markup...

    >
    > You didn't say that originally...
    >
    > foreach (KeyValuePair<string, string> objKVP in dicTest)
    > {
    > MyLabel.Text += objKVP.Key + " - " + objKVP.Value + "<br />";
    > }
    >
    > If the Dictionary is quite large, it would probably be better to use a
    > StringBuilder...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net


    There seems to be a problem with the KeyValuePair<string, string> part. I am
    not getting intelisense for the local variable defined as a KeyValuePair.

    foreach(KeyValuePair<string, string> values in StockContract.Dictionary) {
    Label1.Text = Values.key; //cant get intelisense for this line...

    ....
    }
    Andy B, Apr 23, 2008
    #4
  5. Andy B

    Guest

    Hi Andy,

    I was just trying to do this myself, and had some luck using the
    ItemDataBound event:

    MyRepeater.DataSource = dicTest;
    rptBusinessTypes.ItemDataBound += new
    RepeaterItemEventHandler(MyRepeater_ItemDataBound);
    MyRepeater.DataBind();


    void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e){
    //in the repeater ItemTemplate, you'll have a control
    //called foo. I made mine an asp:Hyperlink.

    HyperLink foo = e.Item.FindControl("foo") as HyperLink;
    if(null != foo)
    {
    DictionaryEntry de = (DictionaryEntry)(e.Item.DataItem);
    foo.Text = Convert.ToString(de.Value);
    }

    *** Sent via Developersdex http://www.developersdex.com ***
    , Sep 24, 2008
    #5
    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. Equis Uno
    Replies:
    6
    Views:
    498
    Equis Uno
    Mar 1, 2004
  2. Florian Lindner

    key - key pairs

    Florian Lindner, Jun 23, 2005, in forum: Python
    Replies:
    8
    Views:
    546
    Paul McGuire
    Jun 24, 2005
  3. Markus Dehmann

    key-value pairs: key consists of 3 ints

    Markus Dehmann, Jan 15, 2006, in forum: C++
    Replies:
    13
    Views:
    632
    Richard Herring
    Jan 23, 2006
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    960
    Øyvind Isaksen
    May 18, 2007
  5. Antonio Quinonez
    Replies:
    2
    Views:
    167
    Antonio Quinonez
    Aug 14, 2003
Loading...

Share This Page