generate <asp:ListItem> with dynamic content

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Blount

    generate <asp:ListItem> with dynamic content

    As mentioned before, I'm creating a multi-lingual page where the text of
    the page comes from a database. This page includes a registration form
    which asks for address information, including the Country.

    I have 6 .txt files that contain a complete list of countries that we
    sell to (65 in total), each written in the appropriate way for the
    language of the site, e.g. Mexico for English US and UK sites and México
    for the Spanish language site.

    I want to add these countries to a ListBox, changing the .Text of the
    ListItem depending on which language the pages is being viewed it.

    The problem I think I'll face is that I will have to manually specify 65
    ListItem tags and then dynamically set the .Value and .Text for each
    one, and that's a lot of work (but if it has to be done, so be it).

    I'm wondering if there might be a way to dynamically add new ListItem
    tags to my ListBox by opening the .txt file and going through it line by
    line until I reach the end.

    I have 3 ways/ideas in mind, and wanted to get feedback on them, and
    potentially any help/examples of other ways:

    1) manually code 65 ListItem tags, 65 "CountryItem45. Value =
    txtFileValueEnt ry;" lines and 65 "CountryItem23. Text =
    txtFileTextEntr y;" lines;

    2) use HTML <selectand <optiontags instead (can I use HTML form tags
    inside my ASP.NET runat=server form that uses asp:TextBox tags already,
    i.e. mix between .NET and HTML in the same form?)

    3) open the .txt file and for each line read create a new ListItem and
    set it's .Value and .Text - this is the preferred, but I've no clue how
    to get started with it.

    any help would be greatly appreciated.

    Kevin
  • Teemu Keiski

    #2
    Re: generate &lt;asp:ListIte m&gt; with dynamic content

    Can you give a hint of contents of one that type of file? essentially you'd
    just read the file with StreamReader using ReadLine method. And in that loop
    add ListItems, something like

    Dim sr As StreamReader = Nothing
    Try
    sr= New StreamReader(pa th)

    Do While sr.Peek() >= 0
    Dim linetext As String = sr.ReadLine()

    'Here get text and value out of the one line string...I
    don't know the file structure so I cannot demonstrate that
    Dim text As String = ...
    Dim value As String = ...

    'Creating and adding ListItem
    Dim litem As New ListItem(text,v alue)
    ListBox1.Items. Add(litem)
    Loop
    Finally
    If Not sr Is Nothing Then sr.Close()
    End try


    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU




    "Kevin Blount" <kevin.blount@L OLgmail.comwrot e in message
    news:%23A77oCFy GHA.4548@TK2MSF TNGP05.phx.gbl. ..
    As mentioned before, I'm creating a multi-lingual page where the text of
    the page comes from a database. This page includes a registration form
    which asks for address information, including the Country.
    >
    I have 6 .txt files that contain a complete list of countries that we sell
    to (65 in total), each written in the appropriate way for the language of
    the site, e.g. Mexico for English US and UK sites and México for the
    Spanish language site.
    >
    I want to add these countries to a ListBox, changing the .Text of the
    ListItem depending on which language the pages is being viewed it.
    >
    The problem I think I'll face is that I will have to manually specify 65
    ListItem tags and then dynamically set the .Value and .Text for each one,
    and that's a lot of work (but if it has to be done, so be it).
    >
    I'm wondering if there might be a way to dynamically add new ListItem tags
    to my ListBox by opening the .txt file and going through it line by line
    until I reach the end.
    >
    I have 3 ways/ideas in mind, and wanted to get feedback on them, and
    potentially any help/examples of other ways:
    >
    1) manually code 65 ListItem tags, 65 "CountryItem45. Value =
    txtFileValueEnt ry;" lines and 65 "CountryItem23. Text = txtFileTextEntr y;"
    lines;
    >
    2) use HTML <selectand <optiontags instead (can I use HTML form tags
    inside my ASP.NET runat=server form that uses asp:TextBox tags already,
    i.e. mix between .NET and HTML in the same form?)
    >
    3) open the .txt file and for each line read create a new ListItem and set
    it's .Value and .Text - this is the preferred, but I've no clue how to get
    started with it.
    >
    any help would be greatly appreciated.
    >
    Kevin

    Comment

    • Kevin Blount

      #3
      Re: generate &lt;asp:ListIte m&gt; with dynamic content

      Hi Teemu,

      Thanks for the reply. Your example shows the bits I needed, namely the
      "ListBox1.Items .Add()" part.

      The text files contains the following type of data:

      esp
      Mexico,México
      Netherlands,Ned erlands

      where the first 'column' is the English spelling of a country, and the
      second 'column' is the localized translation of that country.

      or

      French,Francés
      German,Alemán
      Dutch,Holandés

      where first you get the English spelling of a language, and then the
      localize spelling.

      I'll be using the C# .Split() method to separate the two 'columns', but
      assuming I end up with 2 strings called "engLang" and "localLang" or
      "engCountry " and "engCountry " I think I would be updating your example
      to use:

      countryList.Ite ms.Add(engLang, localLang)

      for example. Would that be right?

      I'm going to start putting this into my code now, so I'll probably get
      it working, or sit waiting for your reply if I fail ;)

      Thanks again

      Kevin

      Teemu Keiski wrote:
      Can you give a hint of contents of one that type of file? essentially you'd
      just read the file with StreamReader using ReadLine method. And in that loop
      add ListItems, something like
      >
      Dim sr As StreamReader = Nothing
      Try
      sr= New StreamReader(pa th)
      >
      Do While sr.Peek() >= 0
      Dim linetext As String = sr.ReadLine()
      >
      'Here get text and value out of the one line string...I
      don't know the file structure so I cannot demonstrate that
      Dim text As String = ...
      Dim value As String = ...
      >
      'Creating and adding ListItem
      Dim litem As New ListItem(text,v alue)
      ListBox1.Items. Add(litem)
      Loop
      Finally
      If Not sr Is Nothing Then sr.Close()
      End try
      >
      >

      Comment

      • sloan

        #4
        Re: generate &lt;asp:ListIte m&gt; with dynamic content


        ListItem li = new ListItem("North Carolina" , "NC");
        ddlMyDropDownLi st.Items.Add(li );

        The trick is the overloaded ListItem constructor.
        or you can set the values like this:

        ListItem lili = new ListItem();
        lili.Text = "my text";
        lili.Value = "MT";

        something like that.





        "Kevin Blount" <kevin.blount@L OLgmail.comwrot e in message
        news:%23A77oCFy GHA.4548@TK2MSF TNGP05.phx.gbl. ..
        As mentioned before, I'm creating a multi-lingual page where the text of
        the page comes from a database. This page includes a registration form
        which asks for address information, including the Country.
        >
        I have 6 .txt files that contain a complete list of countries that we
        sell to (65 in total), each written in the appropriate way for the
        language of the site, e.g. Mexico for English US and UK sites and México
        for the Spanish language site.
        >
        I want to add these countries to a ListBox, changing the .Text of the
        ListItem depending on which language the pages is being viewed it.
        >
        The problem I think I'll face is that I will have to manually specify 65
        ListItem tags and then dynamically set the .Value and .Text for each
        one, and that's a lot of work (but if it has to be done, so be it).
        >
        I'm wondering if there might be a way to dynamically add new ListItem
        tags to my ListBox by opening the .txt file and going through it line by
        line until I reach the end.
        >
        I have 3 ways/ideas in mind, and wanted to get feedback on them, and
        potentially any help/examples of other ways:
        >
        1) manually code 65 ListItem tags, 65 "CountryItem45. Value =
        txtFileValueEnt ry;" lines and 65 "CountryItem23. Text =
        txtFileTextEntr y;" lines;
        >
        2) use HTML <selectand <optiontags instead (can I use HTML form tags
        inside my ASP.NET runat=server form that uses asp:TextBox tags already,
        i.e. mix between .NET and HTML in the same form?)
        >
        3) open the .txt file and for each line read create a new ListItem and
        set it's .Value and .Text - this is the preferred, but I've no clue how
        to get started with it.
        >
        any help would be greatly appreciated.
        >
        Kevin

        Comment

        • Kevin Blount

          #5
          Re: generate &lt;asp:ListIte m&gt; with dynamic content

          Thanks for the response, sloan.

          Following Teemu's post I got busy, and here's the result

          (and it even works!!)

          <% System.IO.Strea mReader sr = new
          System.IO.Strea mReader(Server. MapPath("test.t xt"));

          string nextLine = null;
          string nextText = string.Empty;
          string nextValue = string.Empty;
          int iCommaPos, iLength;
          while ((nextLine = sr.ReadLine()) != null)
          {
          string[] itemProperties = new string[2];
          itemProperties = nextLine.Split( ',');

          nextText = itemProperties[0].Trim();
          nextValue = itemProperties[1].Trim();

          ListItem nextItem = new ListItem(nextTe xt,nextValue);
          member_country. Items.Add(nextI tem);
          }
          sr.Close();
          %> <asp:ListBox ID="member_coun try" runat="server"
          SelectionMode=" single" Rows="1" Width="300"></asp:ListBox>


          plus... I must be actually learning something, cause I was able to take
          Teemu's VB code and change it to C#!! hehe

          thanks both.. have great weekends

          Kevin

          sloan wrote:
          ListItem li = new ListItem("North Carolina" , "NC");
          ddlMyDropDownLi st.Items.Add(li );
          >
          The trick is the overloaded ListItem constructor.
          or you can set the values like this:
          >
          ListItem lili = new ListItem();
          lili.Text = "my text";
          lili.Value = "MT";
          >
          something like that.

          Comment

          Working...