这样的列表显示的内容,都是一样的,每个单元格,以及列表中数据,没有样式的区分
然后就会有这样的需求出现,就是某些字段,需要进行特别处理,字体标红,或者后面增加图片,图标等,可以使用这样的方式处理
页面代码如下:
[C#] 纯文本查看 复制代码 <asp:GridView ID="GVCompany" runat="server" AutoGenerateColumns="false"
PageSize="20">
<Columns>
<asp:BoundField DataField="ComuserID" HeaderText="企业编号" />
<asp:TemplateField HeaderText="广告名称">
<ItemTemplate>
<a href='<%#Eval("logourl")%>' target="_blank">
<asp:Label ID="lblName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</a>
<asp:Label ID="lblReminder" runat="server" Text=''></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="AddDate" HeaderText="更新时间" />
<asp:BoundField DataField="BeginDateName" HeaderText="开始时间" />
<asp:TemplateField HeaderText="截至时间">
<ItemTemplate>
<asp:Label ID="lblEndDate" runat="server" Text='<%#Eval("EndDateName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操 作">
<ItemTemplate>
<a href="javascript:parent.openNewWin('Menu83', '修改广告', 'Ad/AddAd.aspx?AdID=<%#Eval("ID")%>')">
修改</a>
<asp:LinkButton ID="lbDel" runat="server" CommandArgument='<%#Eval("ID")%>'
CommandName="del">删除</asp:LinkButton>
<asp:LinkButton ID="lbRemPause" runat="server" CommandArgument='<%# Eval("ID")+","+Eval("AdFlag") %>'
CommandName="pause">
<%#Eval("AdFlag").ToString()=="已暂停"?"取消暂停":(Eval("AdFlag").ToString()=="已删除"?"":"暂停")%></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
没有数据...</EmptyDataTemplate>
</asp:GridView>
后台代码处理如下:
[C#] 纯文本查看 复制代码 protected void GVCompany_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblReminder = e.Row.FindControl("lblReminder") as Label;
Label lblEndDate = e.Row.FindControl("lblEndDate") as Label;
DateTime endDate = Convert.ToDateTime(lblEndDate.Text);
int date = (Convert.ToDateTime(endDate.ToString("yyyy-MM-dd")) - Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"))).Days;
if (date >= 0 && date <= 1)
{
lblReminder.ForeColor = System.Drawing.Color.Red;
if (date == 0)
{
lblReminder.Text ="(今天到期)";
}
if (date == 1)
{
lblReminder.Text ="(明天到期)";
}
}
}
}
//分页方法添加方法
protected void GVCompany_Init(object sender, EventArgs e)
{
GVCompany.PageIndexChanging += new GridViewPageEventHandler(GVCompany_PageIndexChanging);
GVCompany.RowCommand += new GridViewCommandEventHandler(GVCompany_RowCommand);
}
//行内事件处理
public void GVCompany_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "del")
{
int ID = Convert.ToInt32(e.CommandArgument);
lineAdBLL.Delete(ID);
}
if (e.CommandName == "pause")
{
string type = e.CommandArgument.ToString().Split(',')[1];
int id = Convert.ToInt32(e.CommandArgument.ToString().Split(',')[0]);
Sys_LineAdBLL lineAdBll = new Sys_LineAdBLL();
if (type == "已暂停")
lineAdBll.UpdateStatueById(id, 0);
else
lineAdBll.UpdateStatueById(id, 3);
}
BindData();
}
//分页事件
public void GVCompany_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GVCompany.PageIndex = e.NewPageIndex;
BindData();
}
|