我创建了一个程序,其中使用了
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();
}
我找到了解决方案......
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)
{
repeator.DataSource = dt;
repeator.DataBind();
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
{
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 (viewall_ == false)
{
// if (ViewState["viewall"] != "1")
if (lnk.CommandArgument.ToString() == (PageNumber + 1).ToString())
{
lnk.ForeColor = System.Drawing.Color.Black;
}
else
{
{
lnk.ForeColor = System.Drawing.Color.White;
}
}
}
else
{
if (lnk.CommandArgument.ToString() == "View all".ToString())
{
lnk.ForeColor = System.Drawing.Color.Black;
}
}
}
protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
{
PageNumber = Convert.ToInt32(e.CommandArgument.ToString().Replace("View all","1111")) - 1;
if (PageNumber == 1110)
{
viewall_ = true;
}
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();
}