Solved

Identify a Repeater Item Text value when paging through a GridView

Posted on 2013-01-06
3
418 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now