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

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 [ Di, 22 April 2008 17:55 ] [ ID #1947410 ]

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

"Andy B" <a_borka [at] sbcglobal.net> wrote in message
news:OvPv2EJpIHA.1768 [at] TK2MSFTNGP03.phx.gbl...

>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
mark [ Di, 22 April 2008 18:42 ] [ ID #1947416 ]

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

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]" <mark [at] markNOSPAMrae.net> wrote in message
news:%23AkxqfJpIHA.3684 [at] TK2MSFTNGP05.phx.gbl...
> "Andy B" <a_borka [at] sbcglobal.net> wrote in message
> news:OvPv2EJpIHA.1768 [at] TK2MSFTNGP03.phx.gbl...
>
>>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 [ Mi, 23 April 2008 01:58 ] [ ID #1948302 ]

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

"Andy B" <a_borka [at] sbcglobal.net> wrote in message
news:ub2KzSNpIHA.4112 [at] TK2MSFTNGP03.phx.gbl...

[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
mark [ Mi, 23 April 2008 03:45 ] [ ID #1948305 ]

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 [at] markNOSPAMrae.net> wrote in message
news:O%23X4tOOpIHA.3652 [at] TK2MSFTNGP03.phx.gbl...
> "Andy B" <a_borka [at] sbcglobal.net> wrote in message
> news:ub2KzSNpIHA.4112 [at] TK2MSFTNGP03.phx.gbl...
>
> [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 [ Mi, 23 April 2008 04:18 ] [ ID #1948306 ]

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

"Andy B" <a_borka [at] sbcglobal.net> wrote in message
news:%23zHSIhOpIHA.4112 [at] TK2MSFTNGP03.phx.gbl...

[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 + "
";
}

If the Dictionary is quite large, it would probably be better to use a
StringBuilder...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
mark [ Mi, 23 April 2008 11:20 ] [ ID #1948321 ]

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

"Mark Rae [MVP]" <mark [at] markNOSPAMrae.net> wrote in message
news:uo4KCNSpIHA.1164 [at] TK2MSFTNGP04.phx.gbl...
> "Andy B" <a_borka [at] sbcglobal.net> wrote in message
> news:%23zHSIhOpIHA.4112 [at] TK2MSFTNGP03.phx.gbl...
>
> [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 + "
";
> }
>
> 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 [ Mi, 23 April 2008 17:15 ] [ ID #1948354 ]

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

"Andy B" <a_borka [at] sbcglobal.net> wrote in message
news:%23Q04jTVpIHA.552 [at] TK2MSFTNGP06.phx.gbl...

> 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...

C# is case-sensitive - Values.key is not the same as values.Key...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net
mark [ Mi, 23 April 2008 21:17 ] [ ID #1948388 ]
Microsoft » microsoft.public.dotnet.framework.aspnet » Displaying dictionary collection key/value pairs formatted with html markup on a page

Vorheriges Thema: Exception Handling Problem
Nächstes Thema: is there a way to refer to public properties in other user controls