locked
How to set pagenumber in repeter as List view RRS feed

  • Question

  • User-1578974752 posted

    In Listview ,I can implement the page number and set it to 5,Next question will apper on page 2.But when I do this on repeater,it is not working.Thanks in advance.

    <asp:Datapager ID="dtpgr" runat="server" pagedcontrolid="ListView1" Pagesize="5">

    <Fields>

    <asp:NumericPagerField ButtonCount="5"

    NumericButtonCssClass="numeric_button"

    CurrentPageLabelCssClass="current_page"

    NextPreviousButtonCssClass="next_button" />

    </Fields>

    </asp:DataPager>

      <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource2">

    <itemtemplate>

    <div>

    <td align="left"><%#DataBinder.Eval(Container.DataItem, "Question") %></td>

    </div>

    <table>

    <tr style="background-color:White;">

    <td align="right"><img src="images/Dot.gif"/></td>

    <td align="center"><asp:RadioButton ID="rad1" GroupName="Group1" runat="server" /></td>

    <td align="left"><%#DataBinder.Eval(Container.DataItem, "Option1") %></td>

    <td align="left"><asp:RadioButton ID="rad2" GroupName="Group1" runat="server" OnSelectedIndexChanged="btnSave_Click" /></td>

    <td align="left"><%#DataBinder.Eval(Container.DataItem, "Option2") %></td>

    <td align="center"><asp:RadioButton ID="rad3" GroupName="Group1" runat="server" /></td>

    <td align="left"><%#DataBinder.Eval(Container.DataItem, "Option3") %></td>

    <td align="center"><asp:RadioButton ID="rad4" GroupName="Group1" runat="server" /></td>

    <td align="left"><%#DataBinder.Eval(Container.DataItem, "Option4") %></td>

    <td>

    </td>

    </table>

    </itemtemplate>

    </asp:ListView>

    Wednesday, August 29, 2018 3:30 AM

Answers

  • User632428103 posted

    Hello all

    @shsu => here is it a sample with repeater and dataPager :

    string selectCommand = "SELECT EmployeeId, concat(lastName, firstName) as employeeName, birthDate FROM EMPLOYEES";
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    LoadData();
                }
            }
    
            void LoadData()
            {
                int cnt = 0;
                DataSet ds = new DataSet();
    
                //
                SqlConnection connection = new SqlConnection(GetConnectionString);
                SqlCommand sqlCmd = new SqlCommand(selectCommand, connection);
                SqlDataAdapter adapter = new SqlDataAdapter(sqlCmd);
                adapter.Fill(ds);
    
                //
                cnt = ds.Tables[0].Rows.Count;
    
                //
                PagedDataSource pagedDataSource = new PagedDataSource();
                pagedDataSource.DataSource = ds.Tables[0].DefaultView;
                pagedDataSource.AllowPaging = true;
                pagedDataSource.PageSize = 2;
                pagedDataSource.CurrentPageIndex = PgNumber;
    
                //
                int vcnt = cnt / pagedDataSource.PageSize;
    
                //
                if (PgNumber < 1) lnkPrevisous.Visible = false;
                else if (PgNumber >= 1) lnkPrevisous.Visible = true;
    
                //
                if (PgNumber == vcnt) lnkNext.Visible = false;
                else if (PgNumber < vcnt) lnkNext.Visible = true;
    
                //
                repeaterPager.DataSource = pagedDataSource;
                repeaterPager.DataBind();
            }
    
            public int PgNumber
            {
                get
                {
                    if (ViewState["PgNumber"] != null)
                        return Convert.ToInt32(ViewState["PgNumber"]);
                    else
                        return 0;
                }
                set
                {
                    ViewState["PgNumber"] = value;
                }
            }
            public string GetConnectionString
            {
                get { return ConfigurationManager.ConnectionStrings["NORTHWINDConnectionString"].ConnectionString; }
            }
    
            protected void lnkNext_Click(object sender, EventArgs e)
            {
                PgNumber +=1;
                LoadData();
            }
    
            protected void lnkPrevisous_Click(object sender, EventArgs e)
            {
                PgNumber -= 1;
                LoadData();
            }

    and design like this 

    <asp:Repeater ID="repeaterPager" runat="server">
                <HeaderTemplate>
                    <table border="1">
                        <tr><td>EmployeeID</td><td>employeeName</td><td>BirthDate</td>
                        </tr>
                    </table>
                </HeaderTemplate>
                <ItemTemplate>
                    <table style="background-color:aqua">
                        <tr>
                            <td><%# Eval("EmployeeID") %></td>
                            <td><%# Eval("employeeName") %></td>
                            <td><%# Eval("BirthDate") %></td>
                        </tr>
                    </table>
                </ItemTemplate>
                <AlternatingItemTemplate>
                    <table style="background-color:silver">
                        <tr>
                            <td><%# Eval("EmployeeID") %></td>
                            <td><%# Eval("employeeName") %></td>
                            <td><%# Eval("BirthDate") %></td>
                        </tr>
                    </table>
                </AlternatingItemTemplate>
            </asp:Repeater>
            <br />
            <asp:LinkButton ID="lnkNext" runat="server" Text="Next page" NavigateUrl="/repeaterDataPager.aspx" OnClick="lnkNext_Click"></asp:LinkButton>
            <asp:LinkButton ID="lnkPrevisous" runat="server" Text="Previous Page" NavigateUrl="/repeaterDataPager.aspx" OnClick="lnkPrevisous_Click"></asp:LinkButton

    off course it's an c# but you know now how to convert it in vb :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 30, 2018 9:22 AM

All replies

  • User632428103 posted

    Hello shsu,

    never try but read this sample 

    http://www.dotnetfunda.com/articles/show/980/paging-with-repeater-control

    Wednesday, August 29, 2018 6:20 AM
  • User-1578974752 posted

    In list view it is working fine.But I want in Repeater,Because all the saving codes are place in repeater control.If paging is difficult in Repeater ,I wish to continue with List view but I am using below code for Repeater Save. Below code working fine for repeater .Hence how can I save in to database in List View as below. I tried ..but couldn't do.

    <asp:Repeater ID="repeater" runat="server" DataSourceID="SqlDataSource1"  >
    <HeaderTemplate>
        <table class="table table-striped table-dark table-bordered">
      </HeaderTemplate>
    <ItemTemplate>

    <tr><td align="left" colspan="3"><%#Eval("Question") %> <asp:HiddenField ID="QuestionDetailID" runat="server" Value='<%#Eval("QuestionDetailID") %>' />   </td></tr>
    </div>
    <tr align="left">
      
    <td align="left">     <asp:Label ID="Option1" runat="server" Text=' <%# Eval ("Option1") %>'></asp:Label><asp:RadioButton ID="rad1" GroupName="Group1" runat="server" /></td>
     
    <td align="left"> <asp:Label ID="Option2" runat="server" Text=' <%# Eval("Option2") %>'></asp:Label>   <asp:RadioButton ID="rad2" GroupName="Group1" runat="server"  /></td>

    <td align="left"> <asp:Label ID="Option3" runat="server" Text=' <%# Eval("Option3") %>'></asp:Label>  <asp:RadioButton ID="rad3" GroupName="Group1" runat="server" /></td>
     

     


    </tr>
    </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
    </asp:Repeater>

            <asp:Button ID="Button1" runat="server" Text="save" CssClass="btn btn-primary" OnClick="Button1_Click" />
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EntitiesConnectionString %>" SelectCommand="SELECT [questionDetailId], [option1], [option2], [option3], [question] FROM [questionDetail]"></asp:SqlDataSource>
        </form>

    Code behind.

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
            Dim sql As String = "update questionDetail set correctanswer=@option where questionDetailId=@id"
            For Each ri As RepeaterItem In repeater.Items
                If ri.ItemType = ListItemType.Item Or ri.ItemType = ListItemType.AlternatingItem Then


                    Dim r1 As RadioButton = TryCast(ri.FindControl("rad1"), RadioButton)

                    Dim r2 As RadioButton = TryCast(ri.FindControl("rad2"), RadioButton)

                    Dim r3 As RadioButton = TryCast(r2.FindControl("rad3"), RadioButton)

                    Dim sqlparameterId As SqlParameter = New SqlParameter("id", SqlDbType.Int)
                    'get the id of current questionDetail
                    Dim questdetid As String = TryCast(ri.FindControl("QuestionDetailID"), HiddenField).Value
                    sqlparameterId.Value = Convert.ToInt32(questdetid)


                    If r1.Checked Then
                        'if the first radio is checked,get  text of option1 and set correctAnswer field to the text
                        Dim answer As String = TryCast(r2.FindControl("Option1"), Label).Text
                        Dim sqlParameterAnswer As SqlParameter = New SqlParameter("option", SqlDbType.NVarChar, 100)
                        sqlParameterAnswer.Value = answer
                        ExcuteNunQuery(sql, sqlparameterId, sqlParameterAnswer)

                    End If

                    If r2.Checked Then
                        'if the first radio is checked,get  text of option2 and set correctAnswer field to the text
                        Dim sqlParameterAnswer As SqlParameter = New SqlParameter("option", SqlDbType.NVarChar, 100)

                        Dim answer As String = TryCast(r2.FindControl("Option2"), Label).Text
                        sqlParameterAnswer.Value = answer
                        sqlParameterAnswer.Value = answer
                        ExcuteNunQuery(sql, sqlparameterId, sqlParameterAnswer)
                    End If

                    If r3.Checked Then
                        'if the first radio is checked,get  text of option3 and set correctAnswer field to the text
                        Dim sqlParameterAnswer As SqlParameter = New SqlParameter("option", SqlDbType.NVarChar, 100)

                        Dim answer As String = TryCast(r2.FindControl("Option3"), Label).Text
                        sqlParameterAnswer.Value = answer
                        sqlParameterAnswer.Value = answer
                        ExcuteNunQuery(sql, sqlparameterId, sqlParameterAnswer)
                    End If

                End If
            Next


        End Sub

    Public Function ExcuteNunQuery(ByVal sql As String, ParamArray sqlParameters As SqlParameter()) As Integer
            Using con As SqlConnection = New SqlConnection(url)

                Using com As SqlCommand = New SqlCommand(sql, con)
                    com.Parameters.AddRange(sqlParameters)
                    con.Open()
                    Return com.ExecuteNonQuery()
                End Using
            End Using
        End Function

    Thursday, August 30, 2018 1:10 AM
  • User-893317190 posted

    Hi shsu ,

    You could  try code below. In fact , it is similar to repeater.

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
    
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" ItemPlaceholderID="itemPlaceHolder" DataKeyNames="id">
    
                <LayoutTemplate >
                    <table class="table table-striped  table-bordered ">
                    
                               <thead>
                                    <tr runat="server" style="">
                                     
                                        
                                        <th runat="server">Option1</th>
                                        <th runat="server">Option2</th>
                                        <th runat="server">Option3</th>
                                    </tr>
                                   </thead>
                        <tbody>
    <!--represent the item --> <asp:Panel ID="itemPlaceHolder" runat="server"></asp:Panel> <tr > <td colspan="3" style=""> <asp:DataPager ID="DataPager1" runat="server" PageSize="3"> <Fields> <asp:NumericPagerField ButtonCount="5" NumericButtonCssClass="numeric_button" CurrentPageLabelCssClass="current_page" NextPreviousButtonCssClass="next_button" /> <%-- <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />--%> </Fields> </asp:DataPager> </td> </tr> </tbody> </table> </table> </LayoutTemplate> <ItemTemplate> <tr> <td align="left" colspan="3"><%#Eval("Question") %> <asp:HiddenField ID="QuestionDetailID" runat="server" Value='<%#Eval("ID") %>' /> </td> </tr> </div> <tr align="left"> <td align="left"> <asp:Label ID="Option1" runat="server" Text=' <%# Eval ("Option1") %>'></asp:Label><asp:RadioButton ID="rad1" GroupName="Group1" runat="server" /></td> <td align="left"> <asp:Label ID="Option2" runat="server" Text=' <%# Eval("Option2") %>'></asp:Label> <asp:RadioButton ID="rad2" GroupName="Group1" runat="server" /></td> <td align="left"> <asp:Label ID="Option3" runat="server" Text=' <%# Eval("Option3") %>'></asp:Label> <asp:RadioButton ID="rad3" GroupName="Group1" runat="server" /></td> </tr> </ItemTemplate> </asp:ListView> <asp:Button ID="Button1" runat="server" Text="save" CssClass="btn btn-primary" OnClick="Button1_Click" /> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EntityExeConnectionString %>" SelectCommand="SELECT [id], [Question], [Option1], [Option2], [Option3] FROM [Questions]"></asp:SqlDataSource> </form> </body> </html>

    Code behind.

      Protected Sub Button1_Click(sender As Object, e As EventArgs)
            Dim sql As String = "update questions set correctanswer=@option where Id=@id"
            For Each ri As ListViewItem In ListView1.Items
    
                Dim r1 As RadioButton = TryCast(ri.FindControl("rad1"), RadioButton)
    
                    Dim r2 As RadioButton = TryCast(ri.FindControl("rad2"), RadioButton)
    
                    Dim r3 As RadioButton = TryCast(r2.FindControl("rad3"), RadioButton)
    
                    Dim sqlparameterId As SqlParameter = New SqlParameter("id", SqlDbType.Int)
    
                    Dim questdetid As String = TryCast(ri.FindControl("QuestionDetailID"), HiddenField).Value
                    sqlparameterId.Value = Convert.ToInt32(questdetid)
    
    
                    If r1.Checked Then
    
                        Dim answer As String = TryCast(r2.FindControl("Option1"), Label).Text
                        Dim sqlParameterAnswer As SqlParameter = New SqlParameter("option", SqlDbType.NVarChar, 100)
                        sqlParameterAnswer.Value = answer
                        ExcuteNunQuery(sql, sqlparameterId, sqlParameterAnswer)
    
                    End If
    
                    If r2.Checked Then
    
                        Dim sqlParameterAnswer As SqlParameter = New SqlParameter("option", SqlDbType.NVarChar, 100)
    
                        Dim answer As String = TryCast(r2.FindControl("Option2"), Label).Text
                        sqlParameterAnswer.Value = answer
                        sqlParameterAnswer.Value = answer
                        ExcuteNunQuery(sql, sqlparameterId, sqlParameterAnswer)
                    End If
    
                    If r3.Checked Then
    
                        Dim sqlParameterAnswer As SqlParameter = New SqlParameter("option", SqlDbType.NVarChar, 100)
    
                        Dim answer As String = TryCast(r2.FindControl("Option3"), Label).Text
                        sqlParameterAnswer.Value = answer
    
                        ExcuteNunQuery(sql, sqlparameterId, sqlParameterAnswer)
                    End If
    
    
            Next
    
    
    
        End Sub
        Private Shared constr As String = ConfigurationManager.ConnectionStrings("EntityExeConnectionString").ConnectionString
        Public Function ExcuteNunQuery(ByVal sql As String, ParamArray sqlParameters As SqlParameter()) As Integer
            Using con As SqlConnection = New SqlConnection(constr)
    
                Using com As SqlCommand = New SqlCommand(sql, con)
                    com.Parameters.AddRange(sqlParameters)
                    con.Open()
                    Return com.ExecuteNonQuery()
                End Using
            End Using
        End Function

    Best regards,

    Ackerly Xu

    Thursday, August 30, 2018 9:08 AM
  • User632428103 posted

    Hello all

    @shsu => here is it a sample with repeater and dataPager :

    string selectCommand = "SELECT EmployeeId, concat(lastName, firstName) as employeeName, birthDate FROM EMPLOYEES";
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    LoadData();
                }
            }
    
            void LoadData()
            {
                int cnt = 0;
                DataSet ds = new DataSet();
    
                //
                SqlConnection connection = new SqlConnection(GetConnectionString);
                SqlCommand sqlCmd = new SqlCommand(selectCommand, connection);
                SqlDataAdapter adapter = new SqlDataAdapter(sqlCmd);
                adapter.Fill(ds);
    
                //
                cnt = ds.Tables[0].Rows.Count;
    
                //
                PagedDataSource pagedDataSource = new PagedDataSource();
                pagedDataSource.DataSource = ds.Tables[0].DefaultView;
                pagedDataSource.AllowPaging = true;
                pagedDataSource.PageSize = 2;
                pagedDataSource.CurrentPageIndex = PgNumber;
    
                //
                int vcnt = cnt / pagedDataSource.PageSize;
    
                //
                if (PgNumber < 1) lnkPrevisous.Visible = false;
                else if (PgNumber >= 1) lnkPrevisous.Visible = true;
    
                //
                if (PgNumber == vcnt) lnkNext.Visible = false;
                else if (PgNumber < vcnt) lnkNext.Visible = true;
    
                //
                repeaterPager.DataSource = pagedDataSource;
                repeaterPager.DataBind();
            }
    
            public int PgNumber
            {
                get
                {
                    if (ViewState["PgNumber"] != null)
                        return Convert.ToInt32(ViewState["PgNumber"]);
                    else
                        return 0;
                }
                set
                {
                    ViewState["PgNumber"] = value;
                }
            }
            public string GetConnectionString
            {
                get { return ConfigurationManager.ConnectionStrings["NORTHWINDConnectionString"].ConnectionString; }
            }
    
            protected void lnkNext_Click(object sender, EventArgs e)
            {
                PgNumber +=1;
                LoadData();
            }
    
            protected void lnkPrevisous_Click(object sender, EventArgs e)
            {
                PgNumber -= 1;
                LoadData();
            }

    and design like this 

    <asp:Repeater ID="repeaterPager" runat="server">
                <HeaderTemplate>
                    <table border="1">
                        <tr><td>EmployeeID</td><td>employeeName</td><td>BirthDate</td>
                        </tr>
                    </table>
                </HeaderTemplate>
                <ItemTemplate>
                    <table style="background-color:aqua">
                        <tr>
                            <td><%# Eval("EmployeeID") %></td>
                            <td><%# Eval("employeeName") %></td>
                            <td><%# Eval("BirthDate") %></td>
                        </tr>
                    </table>
                </ItemTemplate>
                <AlternatingItemTemplate>
                    <table style="background-color:silver">
                        <tr>
                            <td><%# Eval("EmployeeID") %></td>
                            <td><%# Eval("employeeName") %></td>
                            <td><%# Eval("BirthDate") %></td>
                        </tr>
                    </table>
                </AlternatingItemTemplate>
            </asp:Repeater>
            <br />
            <asp:LinkButton ID="lnkNext" runat="server" Text="Next page" NavigateUrl="/repeaterDataPager.aspx" OnClick="lnkNext_Click"></asp:LinkButton>
            <asp:LinkButton ID="lnkPrevisous" runat="server" Text="Previous Page" NavigateUrl="/repeaterDataPager.aspx" OnClick="lnkPrevisous_Click"></asp:LinkButton

    off course it's an c# but you know now how to convert it in vb :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 30, 2018 9:22 AM