62,046
社区成员
发帖
与我相关
我的任务
分享
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
GridViewNoticeBind();
}
public void GridViewNoticeBind()
{
// 绑定数据源,NoticeBLL中的GetNotice()方法
NoticeBLL noticeObject = new NoticeBLL();
GridViewNotice.DataSource = noticeObject.GetNotice();
GridViewNotice.DataKeyNames = new string[] { "No" };
GridViewNotice.DataBind();
for (int i = 0; i < GridViewNotice.Rows.Count; i++)
{
if (GridViewNotice.PageIndex == 0)
{
// GridView实现用“...”代替超长字符串
string gInfor = Convert.ToString(noticeObject.GetNotice().Rows[i]["NoticeTitle"].ToString());
this.GridViewNotice.Rows[i].Cells[1].Text = SubStr(gInfor, 10); // 将"公告内容"这列实现
char noticeFlag = Convert.ToChar(noticeObject.GetNotice().Rows[i]["NoticeFlag"].ToString());
if (noticeFlag == '1')
this.GridViewNotice.Rows[i].Cells[3].Text = "是";
else
this.GridViewNotice.Rows[i].Cells[3].Text = "否";
}
else
{
// GridView实现用“...”代替超长字符串
string gInfor = Convert.ToString(noticeObject.GetNotice().Rows[i + GridViewNotice.PageSize * GridViewNotice.PageIndex]["NoticeTitle"].ToString());
this.GridViewNotice.Rows[i].Cells[1].Text = SubStr(gInfor, 10); // 将"公告内容"这列实现
char noticeFlag = Convert.ToChar(noticeObject.GetNotice().Rows[i + GridViewNotice.PageSize * GridViewNotice.PageIndex]["NoticeFlag"].ToString());
if (noticeFlag == '1')
this.GridViewNotice.Rows[i].Cells[3].Text = "是";
else
this.GridViewNotice.Rows[i].Cells[3].Text = "否";
}
}
}
// 实现"..."代替超长字符串的条用方法
public string SubStr(string s, int nLen)
{
if (s.Length <= nLen)
return s;
string newStr = s.Substring(0, nLen);
newStr += "....";
return newStr;
}
<%@ Page Title="" Language="C#" MasterPageFile="~/AdminSite.master" AutoEventWireup="true" CodeFile="SysInfor.aspx.cs" Inherits="SysInfor" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div style="margin-top:3px;">
<div style="width:520px; border-bottom:1px dashed #ccc;">
<h3 style="margin:0,0,5px,10px; width:200px; font-weight:bold;">
===修改系统公告===
</h3>
</div>
<div style="width:530px; margin-top:30px;">
<asp:GridView ID="GridViewNotice" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" CssClass="gridviewText"
onselectedindexchanging="GridViewNotice_SelectedIndexChanged" AllowPaging="True"
AllowSorting="True" PageSize="3"
onrowdatabound="GridViewNotice_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="No" Visible="False" />
<asp:BoundField DataField="NoticeTitle" HeaderText="公告内容">
<ItemStyle Width="40%" />
</asp:BoundField>
<asp:BoundField DataField="NoticeDate" HeaderText="公告时间">
<ItemStyle Width="20%" />
</asp:BoundField>
<asp:BoundField DataField="NoticeFlag" HeaderText="是否显示">
<ItemStyle Width="20%" />
</asp:BoundField>
<asp:ButtonField CommandName="Select" HeaderText="选择" Text="修改">
<ItemStyle Width="10%" />
</asp:ButtonField>
<asp:ButtonField HeaderText="删除" Text="删除" >
<ItemStyle Width="10%" />
</asp:ButtonField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
</div>
</asp:Content>
直接弄个模版列处理
<asp:TemplateField HeaderText="状态" HeaderStyle-Width="4%">
<ItemTemplate>
<%#Eval("Content").ToString().Length>10?Eval("Content").ToString().Substring(0,10)+"...":Eval("Content")%>
</ItemTemplate>
</asp:TemplateField>
public string SubStr(string s, int nLen)
{
if (s.Length <= nLen)
return s;
return s.SubString(0,nLen)+"....";
}