2013-10-17 29 views
0

我已經創建,其中我已經使用repeater控制一個程序。現在在repeater控制我也使用了分頁。當我點擊頁碼時,數字用其他顏色突出顯示。它工作正常。現在我的問題是我也有View all按鈕。當我點擊那個按鈕。我只想改變view all的顏色。不是數字。無論我在做什麼,當我點擊查看所有按鈕頁碼1和Vie牆都突出顯示。你能給我一些建議嗎?我帶了一個鏈接按鈕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(); 
} 
+0

您需要提供一些代碼,說明你已經嘗試過的東西。 – plalx

+0

是否有可能使用Querystring? –

回答

0

我得到了解決....

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(); 
}