我创建了一个程序,我在其中使用了repeater
控制。现在在repeater
控制中,我还使用了分页。当我单击页码时,该页码以其他颜色突出显示。它工作正常。现在我的问题是我也有View all
按钮。当我点击那个按钮时。我只想改变颜色view all
。不是号码。无论我在做什么,当我点击查看所有按钮时,页码 1 和视图墙都会突出显示。你能给我建议吗?我已经使用了一个链接按钮view all
和一个中继器,该中继器具有用于分页的链接按钮。现在我该怎么办?我应该为此使用Javascript
吗?还是通过后端编码来完成?.aspx 文件代码
<div style="overflow: hidden;" class="inner_bold_txt">
<asp:LinkButton ID="lnkviewall" runat="server" Text="View All"
onclick="lnkviewall_Click"></asp:LinkButton> |
<asp:LinkButton ID="lnkviewpaging" runat="server" Text="Back to Paging"
onclick="lnkviewpaging_Click" Visible="false"></asp:LinkButton>
<asp:Repeater ID="rptPaging" runat="server" OnItemDataBound="rptPaging_ItemDataBound" onitemcommand="rptPaging_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="btnPage" style="padding:8px; margin:2px; background:#ffa100; border:solid 1px #666; font:8pt tahoma;"
CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" OnClick="btnPage_Click" ForeColor="White" Font-Bold="True" >
<%# Container.DataItem %>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
</div>
aspx.cs 文件
protected void BindRepeter()
{
DataTable dt = new DataTable();
dt = (DataTable)Session["news"];
PagedDataSource pds = new PagedDataSource();
DataView dv = new DataView(dt);
pds.DataSource = dv;
pds.AllowPaging = true;
pds.PageSize = Convert.ToInt32(drpItems.SelectedValue.ToString());
pds.CurrentPageIndex = PageNumber;
if (PageNumber == 1110)
{
// rptPaging.DataBind();
repeator.DataSource = dt;
repeator.DataBind();
}
else{
if (pds.PageCount > 1)
{
rptPaging.Visible = true;
ArrayList pages = new ArrayList();
pages.Add("View all".ToString());
for (int i = 0; i < pds.PageCount; i++)
pages.Add((i + 1).ToString());
rptPaging.DataSource = pages;
rptPaging.DataBind();
}
else
{
rptPaging.Visible = false;
}
repeator.DataSource = pds;
repeator.DataBind();
}
Session["news"] = dt;
//}
}
public int PageNumber
{
get
{
if (ViewState["PageNumber"] != null)
return Convert.ToInt32(ViewState["PageNumber"]);
else
return 0;
}
set
{
ViewState["PageNumber"] = value;
}
}
protected void rptPaging_ItemDataBound(object source, RepeaterItemEventArgs e)
{
LinkButton lnk = (LinkButton)e.Item.FindControl("btnPage");
// if (ViewState["viewall"] != "1")
if (lnk.CommandArgument.ToString() == (PageNumber + 1).ToString())
{
lnk.ForeColor = System.Drawing.Color.Black;
}
else
{
//if (lnk.CommandArgument.ToString() == "View all")
//{
// lnk.ForeColor = System.Drawing.Color.Black;
//}
//else
{
lnk.ForeColor = System.Drawing.Color.White;
}
}
}
protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
{
PageNumber = Convert.ToInt32(e.CommandArgument.ToString().Replace("View all","1111")) - 1;
//if (PageNumber == 1110)
//{
// ViewState["viewall"] = "1";
//}
//else
//{
// ViewState["viewall"] = null;
//}
//((LinkButton)e.CommandSource).ApplyStyle(mystyle);
//((LinkButton)e.CommandSource).MergeStyle(mystyle);
//((LinkButton)e.CommandSource).ForeColor=System.Drawing.Color.MediumVioletRed;
BindRepeter();
}
protected void lnkreadmore_Click(object sender, EventArgs e)
{
LinkButton btn = (sender as LinkButton);
DataTable dt = new DataTable();
if (Session["news"] != null)
{
Session["news"] = null;
}
dt.Columns.Add("news_id", typeof(int));
DataRow dr;
dr = dt.NewRow();
dr["news_id"] = btn.CommandArgument;
dt.Rows.Add(dr);
Session["news"] = dt;
Response.Redirect("Default2.aspx");
}
protected void lblSort_OnSelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
con.Open();
string sql;
if(drpSort.SelectedIndex==1)
{
sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) order by news_subject asc";
}
else if (drpSort.SelectedIndex == 2)
{
sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) order by news_subject desc";
}
else if (drpSort.SelectedIndex == 3)
{
sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) ORDER BY CONVERT(DateTime, news_start_date,101) asc";
}
else if (drpSort.SelectedIndex == 4)
{
sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) ORDER BY CONVERT(DateTime, news_start_date,101) DESC";
}
else
{
sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) order by news_position_id";
}
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
Session["news"] = dt;
BindRepeter();
con.Close();
}
protected void drpItems_SelectedIndexChanged(object sender, EventArgs e)
{
BindRepeter();
}