DataGrid 的第一行未填充 DataList 数据
我有一个包含 4 列的 DataGrid 和一个包含 5 列的 DataList。 DataList 本身作为另一个单独的列驻留在 DataGrid 中。 一切都很好,除了数据网格中的第一行不显示数据列表的内容。就好像我的数据列表中的所有内容都应该向上推 1 行才能正确显示数据。
我已经验证(在调试期间)数据在数据网格相应第一行的数据列表中显示正常,但它不会在数据网格的第一行上呈现。它开始在数据网格的第二行上呈现。
我已经多次使用标记和代码隐藏来解决这个问题,但无法弄清楚为什么会发生这种情况。任何帮助将不胜感激。
下面是我的代码:
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;
}
I have a DataGrid with 4 columns and a DataList with 5 columns. The DataList resides inside the DataGrid as another separate column by itself.
Everything is coming out fine, except that the first row in the datagrid does not display the contents of the datalist. It's as if everything in my datalist should be pushed up by a 1 row to display the data correctly.
I have validated (during debugging) that the data is coming out fine in the datalist for the corresponding first row of the datagrid, but it doesn't get rendered on the first row of the datagrid. It starts to get rendered on the second row of the datagrid.
I've been over this numerous times with the markup and the codebehind, and can't figure out why this is happening. Any help would be appreciated.
Below is my code:
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>
CODE BEHIND
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
知道了!
在 GetDtlsReferrals 方法中,我添加了以下代码片段,该代码片段生成了我正在查找的显示数据:
Got it!
In the GetDtlsReferrals method, I added the following code snippet which produced the display data I was looking for: