locked
Starting a Datalist at a specific index RRS feed

  • Question

  • User2009701734 posted

    I have created a sort of calendar using a DataList with the RepeatDirection horizontal and the RepeatColumns at 7. Currently I am making it more "calendar" looking by inserting blank rows at the beginning so that my "day 1" starts on the correct day of the week (Sunday starts at index 0, Friday starts at index 5, etc).

    string strFirstDay = month.DayOfWeek.ToString();
    switch (strFirstDay)
         {
            case "Sunday":
               break;
    
            case "Monday":
               blankRow = dt.NewRow();
               dt.Rows.InsertAt(blankRow, 0);
               break;
            case "Tuesday":
               blankRow = dt.NewRow();
               dt.Rows.InsertAt(blankRow, 0);
               blankRow = dt.NewRow();
               dt.Rows.InsertAt(blankRow, 1);
               break;
    //etc ...
    
    
    }

    The Problem that I am having with this method is that anything in my DataList ItemTemplate will show up on those blank rows, where I want them to be completely empty. The only way I can see this working out, is if I was able to have my data start populating at the index that I want. I'm not sure if this is possible, or if I should start working on a different way to display my data in the ItemTemplate, but I figure that someone here has made this same thing work.

    Monday, February 6, 2017 10:39 PM

Answers

  • User-271186128 posted

    Hi RedDragonX,

    If you add that label to your template you will notice that the 1st 3 cells, on your picture, will all say "TESTING!!!" inside. That is what i am trying to remedy. You will also notice that the last 4 cells will *not* have the label inside... which is what i want to accomplish for the 1st blank ones.

    You could use the Visible property to hide the label based on the Time value. Code as below:

       <ItemTemplate>
            <asp:Label runat="server" Text="TESTING!!!" Visible='<%# string.IsNullOrEmpty(Eval("Time").ToString()) ? false:true %>'></asp:Label>
            <br />
            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Time") %>' />
        </ItemTemplate>

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 14, 2017 9:26 AM

All replies

  • User-271186128 posted

    Hi RedDragonX,

    Based on your description, I suggest you could refer to the following code:

           <asp:DataList ID="DataList1" runat="server" BorderColor="#CC9966" BorderStyle="Dashed"
                 RepeatColumns="7" RepeatDirection="Horizontal" RepeatLayout="Table"
                 BorderWidth="1px" CellPadding="4" DataKeyField="ID" GridLines="Both">
                <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                <ItemStyle BackColor="White" ForeColor="#330099" />
                <HeaderTemplate>
                    <tr>
                        <td>Sunday</td>
                        <td>Monday</td>
                        <td>Tuesday</td>
                        <td>Wenesday</td>
                        <td>Thursday</td>
                        <td>Friday</td>
                        <td>Saturday</td>
                    </tr>
                </HeaderTemplate>
                <ItemTemplate>       
                    <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Time") %>' />
                </ItemTemplate>
                <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            </asp:DataList>

    Code behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2] { new DataColumn("ID", typeof(string)), new DataColumn("Time", typeof(string)) });
    int index = 0; //use to add the ID property

    //suppose display the current month for (int i = 1; i <= 28; i++) { DateTime currenttime = new DateTime(2017, 2, i); //When add the first day, based on the dayofweek value to add empty cell before the first day. if (i == 1) { int strFirstDay = (int)currenttime.DayOfWeek; for (int j = 1; j <= strFirstDay; j++) { index = j; dt.Rows.Add(index.ToString(), ""); } } dt.Rows.Add(index.ToString(), currenttime.ToShortDateString()); } DataList1.DataSource = dt; DataList1.DataBind(); }

    Then, the output as below:

    Best regards,
    Dillion

    Tuesday, February 7, 2017 8:55 AM
  • User2009701734 posted

    Dillion,


    Thanks for the help. the way that you have that setup is almost identical to mine. The only difference, really, is that I am using dt.rows.insertat to add the rows to the front after loading the data, where you are using dt.Rows.Add before the data is loaded. The problem is the same in both situations. if you were to add a static label to your item template like :

    <ItemTemplate>
         <asp:Label runat="server" Text="TESTING!!!"></asp:Label>      
         <br />
         <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Time") %>' />
    </ItemTemplate>
    

    <<Sorry, i cant seem to post an image for some reason>>

    If you add that label to your template you will notice that the 1st 3 cells, on your picture, will all say "TESTING!!!" inside. That is what i am trying to remedy. You will also notice that the last 4 cells will *not* have the label inside... which is what i want to accomplish for the 1st blank ones.

    Tuesday, February 7, 2017 10:11 PM
  • User-271186128 posted

    Hi RedDragonX,

    If you add that label to your template you will notice that the 1st 3 cells, on your picture, will all say "TESTING!!!" inside. That is what i am trying to remedy. You will also notice that the last 4 cells will *not* have the label inside... which is what i want to accomplish for the 1st blank ones.

    You could use the Visible property to hide the label based on the Time value. Code as below:

       <ItemTemplate>
            <asp:Label runat="server" Text="TESTING!!!" Visible='<%# string.IsNullOrEmpty(Eval("Time").ToString()) ? false:true %>'></asp:Label>
            <br />
            <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Time") %>' />
        </ItemTemplate>

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 14, 2017 9:26 AM
  • User2009701734 posted

    perfect! thank you so much for that, it is exactly what i needed !

    Tuesday, February 14, 2017 2:36 PM