.aspx中的repeater代码:
<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand" onitemdatabound="Repeater1_ItemDataBound"><HeaderTemplate>
<table class="tab-bg" width="981" border="0">
<tr>
<td width="50">
序号
</td>
<td width="150">
<asp:LinkButton ID="C_Name" runat="server" CommandName="C_Name" Text="课程名称"></asp:LinkButton>
</td>
<td width="100">
<asp:LinkButton ID="name" runat="server" CommandName="name" Text="课程性质"></asp:LinkButton>
</td>
<td width="50">
<asp:LinkButton ID="C_XueFen" runat="server" CommandName="C_XueFen" Text="学分"></asp:LinkButton>
</td>
<td width="130">
<asp:LinkButton ID="C_KaiKeXueNian" runat="server" CommandName="C_KaiKeXueNian" Text="开课学年、学期"></asp:LinkButton>
</td>
<td width="90">
<asp:LinkButton ID="teaname" runat="server" CommandName="teaname" Text="主讲教师"></asp:LinkButton>
</td>
<td width="76">
<asp:LinkButton ID="C_ZongXueShi" runat="server" CommandName="C_ZongXueShi" Text="总学时"></asp:LinkButton>
</td>
<td width="150">
<asp:LinkButton ID="C_KaiKeDanWei" runat="server" CommandName="C_KaiKeDanWei" Text="开课单位"></asp:LinkButton>
</td>
<td width="90">
<asp:LinkButton ID="C_ShouKeBiaoShi" runat="server" CommandName="C_ShouKeBiaoShi" Text="授课标识"></asp:LinkButton>
</td>
<td width="90">
<asp:LinkButton ID="C_KaiKeZhuangTai" runat="server" CommandName="C_KaiKeZhuangTai" Text="开课状态"></asp:LinkButton>
</td>
</tr>
</table>
<table width="981px" border="0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td width="50">
<%#Container.ItemIndex+1 %>
</td>
<td width="150" class="col-cx">
<a href='kcjs.aspx?id=<%#Eval("c_id") %>'><%#Eval("C_Name")%><br /><%#Eval("C_YingWenName")%></a>
</td>
<td width="100">
<%#Eval("name")%>
</td>
<td width="50">
<%#Eval("C_XueFen")%>
</td>
<td width="130">
<%#Eval("C_KaiKeXueNian")%>
</td>
<td width="90">
<%#Eval("teaname")%>
</td>
<td width="76">
<%#Eval("C_ZongXueShi")%>
</td>
<td width="150">
<%#Eval("C_KaiKeDanWei")%>
</td>
<td width="90">
<%#SutStr(Eval("C_ShouKeBiaoShi").ToString())%>
</td>
<td width="90">
<%#Eval("C_KaiKeZhuangTai")%>
</td>
</tr>
<tr>
<td colspan="10" width="">
<img src="images/ti-cx.jpg">
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
.cs文件中,绑定repeater,点击表头排序的代码:
//绑定repeater
private void GetRepeater()
{
DataTable dt = GetDt();//获取数据的方法
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
//实现点击表头排序的重要代码
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
LinkButton li = e.Item.FindControl(e.CommandName.Trim()) as LinkButton;
if (ViewState[e.CommandName.Trim()] == null)
{
ViewState[e.CommandName.Trim()] = "ASC";
li.Text = li.Text + "▲";
}
else
{
if (ViewState[e.CommandName.Trim()].ToString().Trim() == "ASC")
{
ViewState[e.CommandName.Trim()] = "DESC";
if (li.Text.IndexOf("▲") != -1)
{
li.Text = li.Text.Replace("▲", "▼");
}
else
{
li.Text = li.Text + "▼";
}
}
else
{
ViewState[e.CommandName.Trim()] = "ASC";
if (li.Text.IndexOf("▼") != -1)
{
li.Text = li.Text.Replace("▼", "▲");
}
else
{
li.Text = li.Text + "▲";
}
}
}
ViewState["text"] = li.Text;
ViewState["id"] = e.CommandName.Trim();
DataView dv = GetDt().DefaultView;//重新绑定数据源
dv.Sort = e.CommandName.ToString().Trim() + " " + ViewState[e.CommandName.Trim()].ToString().Trim();
Repeater1.DataSource = dv;
Repeater1.DataBind();
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
if (ViewState["id"] != null)
{
LinkButton li = e.Item.FindControl(ViewState["id"].ToString().Trim()) as LinkButton;
li.Text = ViewState["text"].ToString();
}
}
}