未使用DataList数据填充DataGrid的第一行

时间:2012-01-05 00:44:09

标签: datagrid webforms datalist itemdatabound

我有一个包含4列的DataGrid和一个包含5列的DataList。 DataList作为另一个单独的列驻留在DataGrid内部。 一切都很好,除了数据网格中的第一行不显示datalist的内容。就好像我的数据列表中的所有内容都应该向上推一行才能正确显示数据。

我已经验证(在调试期间)数据在数据列表中对于数据网格的相应第一行是正常的,但是它不会在数据网格的第一行上呈现。它开始在数据网格的第二行上呈现。

我已经多次使用标记和代码隐藏,并且无法弄清楚为什么会发生这种情况。任何帮助将不胜感激。

以下是我的代码:

HTML

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="referrals.ascx.cs" Inherits="dpbrokers.dpbrokers.referrals"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<div align="center">
    <asp:Label ID="errormessage" runat="server" Visible="False" /></div>
<asp:DataGrid ID="lstReferrals" runat="server" DataKeyField="ReferringAffiliateID"
    AutoGenerateColumns="false" CellPadding="4" OnItemDataBound="lstReferrals_ItemDataBound">
    <AlternatingItemStyle Font-Size="9pt" CssClass="small" BackColor="#C2D6FA"></AlternatingItemStyle>
    <ItemStyle Font-Size="9pt" CssClass="small" BackColor="White"></ItemStyle>
    <HeaderStyle Font-Size="11pt" Font-Bold="True" ForeColor="White" BackColor="#ABACAD">
    </HeaderStyle>
    <Columns>
        <asp:TemplateColumn HeaderText="Referring Affiliate">
            <ItemTemplate>
                <asp:HyperLink Text='<%# DataBinder.Eval(Container.DataItem, "ReferringAffiliateName") %>'
                    NavigateUrl='<%# EditURL("Referral",DataBinder.Eval(Container.DataItem, "ReferralID").ToString(),"Edit",0) %>'
                    runat="server" ID="Hyperlink1" />
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="SkyCard Awarded">
            <ItemTemplate>
                <asp:Label ID="itemAward" runat="server" Text='<%# IsAwarded(DataBinder.Eval(Container.DataItem, "IsAwarded").ToString()) %>' />
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:BoundColumn DataField="DSMAffiliateName" HeaderText="District Sales Manager">
        </asp:BoundColumn>
        <asp:BoundColumn DataField="DSMAffiliatePhone" HeaderText="DSM Phone"></asp:BoundColumn>
        <asp:TemplateColumn>
            <ItemTemplate>
                <asp:DataList ID="DetailList" ItemStyle-CssClass="small" DataSource="<%# riReferrals %>"
                    runat="server" Width="100%">
                    <HeaderTemplate>
                        <table width="100%" cellpadding="4" border="1">
                            <tr bgcolor="#ABACAD" class="normalwhite">
                                <td width="25%">
                                    Referral Name
                                </td>
                                <td width="20%">
                                    Phone Number
                                </td>
                                <td width="30%">
                                    Email
                                </td>
                                <td>
                                    Referred On
                                </td>
                                <td>
                                    Is Member?
                                </td>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr class="small">
                            <td>
                                <asp:HyperLink Text='<%# DataBinder.Eval(Container.DataItem, "ContactName") %>' NavigateUrl='<%# EditURL("ReferralID",DataBinder.Eval(Container.DataItem, "ReferralID").ToString(),"Edit",0)  %>'
                                    runat="server" ID="Hyperlink2" />
                            </td>
                            <td>
                                <%# FormatPhone(DataBinder.Eval(Container.DataItem,"Phone").ToString()) %>
                            </td>
                            <td>
                                <%# FormatForEmail(DataBinder.Eval(Container.DataItem, "Email").ToString())%>
                            </td>
                            <td>
                                (<%# DataBinder.Eval(Container.DataItem,"Created", "{0:g}") %>)
                            </td>
                            <td>
                                (<%# IsMember(DataBinder.Eval(Container.DataItem, "JoinedON").ToString())%>)
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:DataList>
                <div align="right">
                    <asp:ImageButton ID="Button1" CommandName="award" ImageUrl="~/images/dpbbuttons/awardbutton.gif"
                        AlternateText="Award SkyCard" runat="server" /></div>
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
    <PagerStyle Mode="NumericPages"></PagerStyle>
</asp:DataGrid>

代码背后

public ArrayList riReferrals;


            protected void Page_Load(System.Object sender, System.EventArgs e)
            {
                if (!Page.IsPostBack)
                    LoadReferrals();
            }


            private void LoadReferrals()
            {
                try
                {
                    ReferralController objReferral = new ReferralController();
                    lstReferrals.DataSource = objReferral.GetReferrals();
                    lstReferrals.DataBind();
                }
                catch (Exception ex)
                {
                    Response.Write(ex);

                    CLogError clsLogError = new CLogError(ex, Request, Session, Context.User.Identity.Name);
                    clsLogError.LogError();
                }
            }

            public void lstReferrals_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            {

                switch (e.Item.ItemType)
                {
                    case ListItemType.Item:
                    case ListItemType.AlternatingItem:
                        lbl1 = (Label)e.Item.FindControl("itemAward");

                        GetDtlReferrals(Convert.ToInt32(lstReferrals.DataKeys[e.Item.ItemIndex]));

                        button.Visible = true;
                        if (lbl1.Text.ToLower() == "No")
                            button.Attributes.Add("onClick", "javascript:return confirm(\'Are You Sure You Wish To Award a SkyCard to this Agent ?\');");
                        else
                        {
                            button.ImageUrl = "~/images/dpbbuttons/revokebutton.gif";
                            button.Attributes.Add("onClick", "javascript:return confirm(\'Are You Sure You Wish To Revoke the SkyCard Award for this Agent ?\');");
                        }

                        break;
                }
            }

            public ArrayList GetDtlReferrals(Int32 KeyField)
            {
                riReferrals = null;

                try
                {
                    // Obtain a list of discussion messages for the module
                    ReferralController objReferral = new ReferralController();
                    riReferrals = objReferral.GetReferralsByAgentID(KeyField);
                }
                catch (Exception ex)
                {
                    Response.Write(ex);

                    CLogError clsLogError = new CLogError(ex, Request, Session, Context.User.Identity.Name);
                    clsLogError.LogError();
                }

                return riReferrals;
            }

1 个答案:

答案 0 :(得分:0)

知道了!

在GetDtlsReferrals方法中,我添加了以下代码片段,它产生了我正在寻找的显示数据:

DataList DetailList = (DataList)e.Item.FindControl("DetailList");
                    DetailList.DataBind();