views:

315

answers:

6

Hello

How do I write out my array with a repeater?

string[,] month = { {"Januari", "Februari", "Mars", "Apri", "Maj", "Juni", "Juli", "Agusti", "September", "November", "Oktober", "December"},
                    {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}
                  };

Now I can use Container.DataItem, but how do I get the first string of items in one place and the second in another place?

<a href="Default.aspx?m=01">Januari</a>
+4  A: 

Looks like you should be using enums in this case... ie...


enum Month = {January=1, February, March};

Month month = Month.January;
Month alsomonth = (Month)(1); // Should work

apandit
+1  A: 

I would recommend using 2 seperate arrays...

string[] month = {"Januari", "Februari"} // blah blah

string[] day = {"01", "02" } // blah blah

Are you working with real dates or is this just your example? Januari???

J.13.L
This may surprise you, but some people speak a different language than English.
Aistina
real dates, swedish format
Frozzare
@Aistina, I don't think @J.13.L was taking a crack at the OP's language. It seemed more like curiosity.
Michael Meadows
oh... I am sorry I didn't mean any offense. I was just asking a clarification question. swedish dates, now I know. That's pretty cool!
J.13.L
+1 I learned something today...
J.13.L
A: 

Consider using Dictionaries (there it a very convenient initialization syntax) or enums.

Dario
A: 

You'd better create an enum if you are really using it for Months. Then you can use <%# ((MonthsEnum)Container.DataItem).ToString() %> for the name and <%# (int)Container.DataItem %> for the number

Stilgar
+3  A: 

It would be possible to use an array of arrays as data source (but not a two dimensional array), but you would have to arrange it the other way arround, so that each inner array would contain a name and a number.

I would prefer a more OOP approach though, it gives a bit more structure and the repeater code gets a lot cleaner:

Create a class for the month information:

public class MonthInfo {

   public string Name { get; private set; }
   public string Number { get; private set; }

   public MonthInfo(string name, string number) {
      Name = name;
      Number = number;
   }

}

Now you can create an array of objects to use as data source for the repeater:

MonthInfo[] months = {
   new MonthInfo("Januari", "01"),
   new MonthInfo("Februari", "02"),
   new MonthInfo("Mars", "03"),
   new MonthInfo("April", "04"), // inte "Apri" ;)
   new MonthInfo("Maj", "05"),
   new MonthInfo("Juni", "06"),
   new MonthInfo("Juli", "07"),
   new MonthInfo("Augusti", "08"), // inte "Agusti" ;)
   new MonthInfo("September", "09"),
   new MonthInfo("Oktober", "10"), // kommer före november ;)
   new MonthInfo("November", "11"),
   new MonthInfo("December", "12")
};

MonthRepeater.DataSource = months;

In the repeater you use the properties of the month info class:

<asp:Repeater runat="server" id="MonthRepeater">
   <ItemTemplate>
      <a href="Default.aspx?m=<%#Eval("Number")>"%><%#Eval("Name")%></a>
   </ItemTemplate>
</asp:Repeater">
Guffa
Det heter Augusti också, med "u" på båda sidor om "g" ;o)
Fredrik Mörk
Det har du rätt i. :)
Guffa
"MonthIngo" should be "MonthInfo"
Bobby Cannon
December = 12 inte 11 :)
Frozzare
Corrected both. / Rättat båda. :)
Guffa
+1  A: 

Here's a globalized version that avoids having to make a class etc....

protected void Page_Load(object sender, EventArgs e) {
     this.Culture = "sv-SE";
     var monthNames = System.Globalization.DateTimeFormatInfo.CurrentInfo.MonthNames.ToList();
     this.MonthRepeater.DataSource = from month in monthNames
                                select new {
                                    Number = monthNames.IndexOf(month) + 1,
                                    Name = month
                                };
     this.MonthRepeater.DataBind();

}

ASPX:

<asp:Repeater runat="server" id="MonthRepeater">       
   <ItemTemplate>
      <a href="Default.aspx?m<%#Eval("Number")%>"><%#Eval("Name")%></a> 
   </ItemTemplate>
</asp:Repeater>
Richard Hein