Solved

Identify a Repeater Item Text value when paging through a GridView

Posted on 2013-01-06
3
426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

687 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