简体   繁体   English

使用嵌套中继器时发生异常

[英]Exception while using nested repeaters

I have a datatable containing all my data called dtData . 我有一个包含所有数据的数据表dtData That datatable contains task descriptions with a task due date for every task description. 该数据表包含任务描述以及每个任务描述的任务截止日期。

Here is what I want to do: The user selects the month and year from a dropdown and clicks on a button. 这是我要执行的操作:用户从下拉列表中选择月份和年份,然后单击一个按钮。 Depending upon the number of days in the selected month, a number of panels are generated. 根据所选月份中的天数,将生成许多面板。 (If there are 30 days, 30 panels are generated). (如果有30天,将生成30个面板)。

Each panel would correspond to a day in the month and would display the date accordingly. 每个面板将对应一个月中的一天,并相应地显示日期。 If the date displayed on the panel matches the task due date for my data, then the corresponding task description is to be displayed in the panel. 如果面板上显示的日期与我的数据的任务到期日期相匹配,则相应的任务描述将显示在面板上。

I have been able to render the calendar view as I want it but for some reason, while trying the following code to display the necessary task descriptions, a NullReferenceException is thrown. 我已经能够根据需要渲染日历视图,但是由于某种原因,在尝试以下代码来显示必要的任务描述时,抛出了NullReferenceException I debugged the code and the exception is thrown on the following line for the second time the loop which contains it is run (It runs perfectly fine the for the first time): 我调试了代码, 第二次在该行上引发了异常(包含该循环的循环第一次运行了):

DateTime p_time = Convert.ToDateTime(((System.Data.DataRowView)(e.Item.DataItem)).Row.ItemArray[1]);

Here is my complete code: 这是我完整的代码:

My aspx: 我的aspx:

<asp:Repeater ID="rptr_timeline" runat="server" OnItemDataBound="GetChildData">

    <ItemTemplate>

        <asp:Panel ID="pnl_timeline" runat="server" BackColor="LightGray" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px">

        &nbsp;<span id="span_day">Day</span>&nbsp;<asp:Label ID="lbl_day_number" runat="server" Text='<%# Eval("Day_Number").ToString() %>'></asp:Label>&nbsp;&nbsp;

        <span id="span_date">Date:</span>&nbsp;<asp:Label ID="lbl_day_date" runat="server" Text='<%# Eval("Day_Date").ToString() %>'></asp:Label>&nbsp;&nbsp;

        <asp:Label ID="lbl_day_name" runat="server" Text='<%# Eval("Day_Name").ToString() %>'></asp:Label><br />

        <asp:Repeater ID="rptr_tasks" runat="server">
            <ItemTemplate>
                <asp:Label ID="lbl_task_name" runat="server" Text='<%# Eval("taskdescription_responsible").ToString() %>'></asp:Label>
            </ItemTemplate>
            <SeparatorTemplate>
                <br /><br />
            </SeparatorTemplate>
        </asp:Repeater>

        </asp:Panel>

    </ItemTemplate>
    <SeparatorTemplate>
        <br />
    </SeparatorTemplate>
</asp:Repeater>

And here is my code behind: 这是我的代码背后:

protected void Load_Dateline(object sender, EventArgs e)
{
    try
    {
        int counter = 0;
        int months_days_number = 0;
        int month_selected = 0;
        int year_selected = 0;

        month_selected = Convert.ToInt32(drpdwn_month.SelectedItem.Value);
        year_selected = Convert.ToInt32(drpdwn_year.SelectedItem.Value);

        months_days_number = DateTime.DaysInMonth(year_selected, month_selected);

        DataTable dtMonthdays = new DataTable();
        dtMonthdays.Columns.Add("Day_Number");
        dtMonthdays.Columns.Add("Day_Date");
        dtMonthdays.Columns.Add("Day_Name");
        dtMonthdays.Columns.Add("ProperDate");

        for (counter = 1; counter <= months_days_number; counter++)
        {
            DataRow new_row = dtMonthdays.NewRow();
            if (counter < 10)
            {
                new_row["Day_Number"] = "0" + counter.ToString();
            }
            else
            {
                new_row["Day_Number"] = counter.ToString();
            }
            new_row["Day_Date"] = counter.ToString() + "/" + drpdwn_month.SelectedItem.Value.ToString() + "/" + year_selected.ToString();

            DateTime temp_date = new DateTime(year_selected, month_selected, counter);

            new_row["Day_Name"] = temp_date.ToString("dddd");

            dtMonthdays.Rows.Add(new_row);

        }

        rptr_timeline.DataSource = dtMonthdays;
        rptr_timeline.DataBind();
    }
    catch (Exception ex)
    {
        lbl_error.Text = "Something went wrong!<br /><br />" + ex.ToString();
    }
}

And the following is called on OnItemDataBound of parent repeater: 然后在父转发器的OnItemDataBound上调用以下命令

protected void GetChildData(Object sender, RepeaterItemEventArgs e)
{
    Repeater nestedRepeater = e.Item.FindControl("rptr_tasks") as Repeater;
    DataTable dt_new = dtData.Clone();

    DateTime p_time = Convert.ToDateTime(((System.Data.DataRowView)(e.Item.DataItem)).Row.ItemArray[1]);

    foreach (DataRow dr in dtData.Rows)
    {
        if (DateTime.Parse(dr["taskduedate_responsible"].ToString()).Equals(p_time.ToString()))
        {
            dt_new.ImportRow(dr);
        }
    }

    if (dt_new != null && dt_new.Rows.Count != 0)
    {
        nestedRepeater.DataSource = dt_new;
        nestedRepeater.DataBind();
    }
}

add check whether currently binding item is an Item. 添加检查当前绑定的项目是否是一个项目。 or AlternateItem in your GetChildData method 或GetChildData方法中的AlternateItem

protected void GetChildData(Object sender, RepeaterItemEventArgs e)
{
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
  {
    //Do binding
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM