Solved

Identify a Repeater Item Text value when paging through a GridView

Posted on 2013-01-06
3
422 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Not showing JavaScript in the list 5 53
ASP.net VB.net Errors when sending data to SQL 5 20
Code works but it's slow 28 70
Regex validation 2 24
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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 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