0

repeaterコントロールを使用したプログラムを 1 つ作成しました。これで、repeaterページングも使用できるようになりました。ページ番号をクリックすると、その番号が他の色で強調表示されます。そして、それはうまくいきます。今私の問題は、View allボタンも持っていることです。そのボタンをクリックすると。のみの色を変更したいview all 。番号ではありません。そして、私が何をしていても、[すべて表示] ボタンをクリックすると、ページ番号 1 と Vie ウォールの両方が強調表示されます。提案を教えてください。私は 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();
}
4

1 に答える 1

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();
}
于 2013-10-19T03:13:10.967 に答える