Solved

Identify a Repeater Item Text value when paging through a GridView

Posted on 2013-01-06
3
424 Views
Last Modified: 2013-01-07
I have  GridView that I use to show a grid of book titles and related information.  I have a Repeater Control on the same page that displays an alphabet list of LinkButtons that is used to control the group of books shown.  In short when I select the "B" link button all I see is a grid of books that have titles starting with "B."

My problem comes when the number of books in an alphabetic group exceeds my GridView property PageSize="15".  When paging beyond the first page I apparently need to reacquire the Text value of the already selected LinkButton in the Repeater control so that I can place it in the database Select statement.   How does the GridView1_PageIndexChanging know what LinkButton.Text value to put into 'myAlpha' in the Select statement?

       <asp:Repeater ID="FilteringUI" runat="server"  
               onitemcommand="FilteringUI_ItemCommand" >
            <ItemTemplate>
                <asp:LinkButton runat="server"  ID="lnkFilter"  
                                Text='<%# Container.DataItem %>'
                                CommandName='<%# Container.DataItem %>' >
                      </asp:LinkButton>
            </ItemTemplate>
         </asp:Repeater>

    <asp:GridView ID="GridView1" runat="server"  
        AutoGenerateColumns="False" AllowPaging="True"  PageSize="15"
            PagerSettings-PageButtonCount="25"
             OnPageIndexChanging="GridView1_PageIndexChanging"
            PagerSettings-Mode="NumericFirstLast"  
             PagerSettings-Position="TopAndBottom"  
             >
        <Columns>
            <asp:BoundField  HeaderText="title" DataField="titlelink" />
            <asp:BoundField DataField="imagelink" />
            <asp:BoundField HeaderText="Description" DataField="desc" />
        </Columns>
    </asp:GridView>
   
   private void BindFilteringUI()
    {
        string[] filterOptions = { "1-9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",  "Q", "R", "S", "T", "U", "V", "W", "Y" };
        FilteringUI.DataSource = filterOptions;
        FilteringUI.DataBind();
    }

    protected void Grid_Fill()
    {
        string selectSQL;

        selectSQL = "select title, titlelink + author as titlelink, imagelink, [desc] ";
        selectSQL += " from tblbooks ";
        selectSQL += " where left(title, 1) " + myAlpha + " order by title ";

        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(connectionString);

        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand(selectSQL, con);
            SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
            sqlDa.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Fetch Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            con.Close();
        }
    }

    protected void FilteringUI_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "1-9":
                myAlpha = " in ('0','1','2','3','4','5','6','7','8','9') ";
                break;
            case "A":
                myAlpha = " ='A' ";
                break;
            case "B":
                myAlpha = " ='B' ";
                break;
            case "C":
                myAlpha = " ='C' ";
                break;
          // etc. . . .
          }

}

    protected void GridView1_PageIndexChanging(object sender, EventArgs e)
    {
       //essentially the same as Grid_Fill()

            string selectSQL;
                selectSQL = "select title, titlelink + author as titlelink, imagelink, [desc] ";
                selectSQL += " from tblbooks ";
                selectSQL += " where left(title, 1) " + myAlpha + " order by title ";

            DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection(connectionString);

            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(selectSQL, con);
                SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
                sqlDa.Fill(dt);

                if (dt.Rows.Count > 0)
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
            finally
            {
                con.Close();
            }
    }
0
Comment
Question by:elyons1955
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
informaniac earned 300 total points
ID: 38749674
If myAlpha is declared a global variable then it's value should be retained.

If it's not retained you can store it in a ViewState variable

ViewState["myAlpha"] = " ='"+e.CommandName+"' ";

selectSQL += " where left(title, 1) " + ViewState["myAlpha"].ToString() + " order by title ";
0
 

Author Comment

by:elyons1955
ID: 38752993
Using the ViewState works.  I did find that putting the ViewState variable directly into the selectSQL statement did not seem to work.  The error suggested that I needed to declare a new object.  I declared a string variable, assigned the ViewState variable to the string variable and used the string variable in the selectSQL statement.   Problem solved and now the paging all works!!
0
 

Author Closing Comment

by:elyons1955
ID: 38753001
A good simple solution that worked with only a minor modification:  the ViewState variable did not work well directly in the selectSQL statement.  By assigning the ViewState variable value to a string variable and using the string variable in the selectSQL statement all worked well.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question