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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy B

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

    I have the object property StockContract.D ictionary which is a dictionary
    collection of <string, stringkey/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?



  • Mark Rae [MVP]

    #2
    Re: Displaying dictionary collection key/value pairs formatted with html markup on a page

    "Andy B" <a_borka@sbcglo bal.netwrote in message
    news:OvPv2EJpIH A.1768@TK2MSFTN GP03.phx.gbl...
    >I have the object property StockContract.D ictionary which is a dictionary
    >collection of <string, stringkey/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" AutoGenerateCol umns="true" />

    Dictionary<stri ng, stringdicTest = new Dictionary<stri ng, string>();
    dicTest.Add("1s t", "First");
    dicTest.Add("2n d", "Second");
    dicTest.Add("3r d", "Third");
    gvTest.DataSour ce = dicTest;
    gvTest.DataBind ();


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • Andy B

      #3
      Re: Displaying dictionary collection key/value pairs formatted with html markup on a page

      I tried to put the StockContract.D ictionary property as a
      listView.DataSo urce. 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]" <mark@markNOSPA Mrae.netwrote in message
      news:%23AkxqfJp IHA.3684@TK2MSF TNGP05.phx.gbl. ..
      "Andy B" <a_borka@sbcglo bal.netwrote in message
      news:OvPv2EJpIH A.1768@TK2MSFTN GP03.phx.gbl...
      >
      >>I have the object property StockContract.D ictionary which is a dictionary
      >>collection of <string, stringkey/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" AutoGenerateCol umns="true" />
      >
      Dictionary<stri ng, stringdicTest = new Dictionary<stri ng, string>();
      dicTest.Add("1s t", "First");
      dicTest.Add("2n d", "Second");
      dicTest.Add("3r d", "Third");
      gvTest.DataSour ce = dicTest;
      gvTest.DataBind ();
      >
      >
      --
      Mark Rae
      ASP.NET MVP
      http://www.markrae.net

      Comment

      • Mark Rae [MVP]

        #4
        Re: Displaying dictionary collection key/value pairs formatted with html markup on a page

        "Andy B" <a_borka@sbcglo bal.netwrote in message
        news:ub2KzSNpIH A.4112@TK2MSFTN GP03.phx.gbl...

        [top-posting corrected]
        >>>I have the object property StockContract.D ictionary which is a dictionary
        >>>collection of <string, stringkey/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:GridVie w ID="gvTest" runat="server" AutoGenerateCol umns="true" />
        >>
        >Dictionary<str ing, stringdicTest = new Dictionary<stri ng, string>();
        >dicTest.Add("1 st", "First");
        >dicTest.Add("2 nd", "Second");
        >dicTest.Add("3 rd", "Third");
        >gvTest.DataSou rce = dicTest;
        >gvTest.DataBin d();
        >
        I tried to put the StockContract.D ictionary property as a
        listView.DataSo urce. 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


        Comment

        • Andy B

          #5
          Re: Displaying dictionary collection key/value pairs formatted with html markup on a page

          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]" <mark@markNOSPA Mrae.netwrote in message
          news:O%23X4tOOp IHA.3652@TK2MSF TNGP03.phx.gbl. ..
          "Andy B" <a_borka@sbcglo bal.netwrote in message
          news:ub2KzSNpIH A.4112@TK2MSFTN GP03.phx.gbl...
          >
          [top-posting corrected]
          >
          >>>>I have the object property StockContract.D ictionary which is a
          >>>>dictionar y collection of <string, stringkey/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:GridVi ew ID="gvTest" runat="server" AutoGenerateCol umns="true" />
          >>>
          >>Dictionary<st ring, stringdicTest = new Dictionary<stri ng, string>();
          >>dicTest.Add(" 1st", "First");
          >>dicTest.Add(" 2nd", "Second");
          >>dicTest.Add(" 3rd", "Third");
          >>gvTest.DataSo urce = dicTest;
          >>gvTest.DataBi nd();
          >>
          >I tried to put the StockContract.D ictionary property as a
          >listView.DataS ource. 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

          Comment

          • Mark Rae [MVP]

            #6
            Re: Displaying dictionary collection key/value pairs formatted with html markup on a page

            "Andy B" <a_borka@sbcglo bal.netwrote in message
            news:%23zHSIhOp IHA.4112@TK2MSF TNGP03.phx.gbl. ..

            [top-posting corrected again]
            >>>>>I have the object property StockContract.D ictionary which is a
            >>>>>dictiona ry collection of <string, stringkey/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:GridVie w ID="gvTest" runat="server" AutoGenerateCol umns="true" />
            >>>>
            >>>Dictionary<s tring, stringdicTest = new Dictionary<stri ng, string>();
            >>>dicTest.Add( "1st", "First");
            >>>dicTest.Add( "2nd", "Second");
            >>>dicTest.Add( "3rd", "Third");
            >>>gvTest.DataS ource = dicTest;
            >>>gvTest.DataB ind();
            >>>
            >>I tried to put the StockContract.D ictionary property as a
            >>listView.Data Source. 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<s tring, stringobjKVP 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


            Comment

            • Andy B

              #7
              Re: Displaying dictionary collection key/value pairs formatted with html markup on a page


              "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
              news:uo4KCNSpIH A.1164@TK2MSFTN GP04.phx.gbl...
              "Andy B" <a_borka@sbcglo bal.netwrote in message
              news:%23zHSIhOp IHA.4112@TK2MSF TNGP03.phx.gbl. ..
              >
              [top-posting corrected again]
              >
              >>>>>>I have the object property StockContract.D ictionary which is a
              >>>>>>dictionar y collection of <string, stringkey/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:GridVi ew ID="gvTest" runat="server" AutoGenerateCol umns="true" />
              >>>>>
              >>>>Dictionary< string, stringdicTest = new Dictionary<stri ng, string>();
              >>>>dicTest.Add ("1st", "First");
              >>>>dicTest.Add ("2nd", "Second");
              >>>>dicTest.Add ("3rd", "Third");
              >>>>gvTest.Data Source = dicTest;
              >>>>gvTest.Data Bind();
              >>>>
              >>>I tried to put the StockContract.D ictionary property as a
              >>>listView.Dat aSource. 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<s tring, stringobjKVP 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<st ring, stringpart. I am
              not getting intelisense for the local variable defined as a KeyValuePair.

              foreach(KeyValu ePair<string, stringvalues in StockContract.D ictionary) {
              Label1.Text = Values.key; //cant get intelisense for this line...

              ....
              }



              Comment

              • Mark Rae [MVP]

                #8
                Re: Displaying dictionary collection key/value pairs formatted with html markup on a page

                "Andy B" <a_borka@sbcglo bal.netwrote in message
                news:%23Q04jTVp IHA.552@TK2MSFT NGP06.phx.gbl.. .
                There seems to be a problem with the KeyValuePair<st ring, stringpart. I
                am not getting intelisense for the local variable defined as a
                KeyValuePair.
                >
                foreach(KeyValu ePair<string, stringvalues in StockContract.D ictionary) {
                Label1.Text = Values.key; //cant get intelisense for this line...
                C# is case-sensitive - Values.key is not the same as values.Key...


                --
                Mark Rae
                ASP.NET MVP


                Comment

                • sofacles@yahoo.com

                  #9
                  Re: Displaying dictionary collection key/value pairs formatted with html markup on a page

                  Hi Andy,

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

                  MyRepeater.Data Source = dicTest;
                  rptBusinessType s.ItemDataBound += new
                  RepeaterItemEve ntHandler(MyRep eater_ItemDataB ound);
                  MyRepeater.Data Bind();


                  void MyRepeater_Item DataBound(objec t sender, RepeaterItemEve ntArgs e){
                  //in the repeater ItemTemplate, you'll have a control
                  //called foo. I made mine an asp:Hyperlink.

                  HyperLink foo = e.Item.FindCont rol("foo") as HyperLink;
                  if(null != foo)
                  {
                  DictionaryEntry de = (DictionaryEntr y)(e.Item.DataI tem);
                  foo.Text = Convert.ToStrin g(de.Value);
                  }

                  *** Sent via Developersdex http://www.developersdex.com ***

                  Comment

                  Working...