[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Identify a Repeater Item Text value when paging through a GridView

Posted on 2013-01-06
3
Medium Priority
?
428 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 1200 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

649 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