DataGrid paging not working on the DataBase-Example

Last post 11-13-2006, 12:52 PM by Adam. 1 replies.
Sort Posts: Previous Next
  •  11-13-2006, 12:25 PM 24279

    DataGrid paging not working on the DataBase-Example

    I have not been able to impliment paging on the Datagrid in the Database-Example which comes with CuteEditor.  The page numbers show and there are about 20 records. when I click on page 2 the screen flashes but the data displayed is still page 1.  Please tell me what I am doing wrong?
     
    I added the following parameters to MyDataGrid:
    AllowPaging="True"
    AllowCustomPaging="True"
    PageSize="10"
     
    and made sure the following was added to the onpage load event:  I used 20 since my list has 20 items.

    Sub Page_Load(Source as Object, E as EventArgs)
    If Not Page.IsPostBack Then
    MyDataGrid.VirtualItemCount = "20"
    BindData()
    End If
    End Sub

     
    thanks
    Milton
     
     
     
    <%@ Page Language="vb" Debug="true"%>

    <%@ Import Namespace="System.Data" %>

    <%@ Import Namespace="System.Data.OleDb" %>

    <%@ Register TagPrefix="cutesoft" TagName="banner" Src="banner.ascx" %>

    <%@ Register TagPrefix="cutesoft" TagName="leftmenu" Src="leftmenu.ascx" %>

    <%@ Register TagPrefix="CE" Namespace="CuteEditor" Assembly="CuteEditor" %>

    <html>

    <head>

    <title>ASP and ASP.NET WYSIWYG Editor - Database Example</title>

    <link rel="stylesheet" href="../example.css" type="text/css" />

    </head>

    <body>

    <form runat="server">

    <cutesoft:banner id="banner1" runat="server" />

    <table border="0" cellpadding="0" cellspacing="0">

    <tr>

    <td width=10 nowrap></td>

    <td valign="top" nowrap id="leftcolumn" width="160">

    <cutesoft:leftmenu id="leftmenu1" runat="server" />

    </td>

    <td width="20" nowrap></td>

    <td valign="top" width="760">

    <b>Database Example</b>

    <asp:Datagrid runat="server"

    Id="MyDataGrid"

    cellpadding="4"

    Headerstyle-BackColor="#eeeeee"

    Headerstyle-Font-Bold="True"

    Width=720px

    Font-Size="12px"

    AutogenerateColumns="False"

    AllowPaging="True"

    AllowCustomPaging="True"

    PageSize="10"

    PagerStyle-Mode="NumericPages"

    OnPageIndexChanged="MyDataGrid_PageChanger"

    OnItemCommand="UpdateItem"

    Font-Names="Arial"

    ForeColor="#333333"

    GridLines="None">

    <Columns>

    <asp:BoundColumn DataField="EventID" Visible="False" />

    <asp:BoundColumn DataField="EventID" HeaderText="ID" >

    <ItemStyle Width="50px" />

    </asp:BoundColumn>

    <asp:BoundColumn DataField="Notes" HeaderText="Note" >

    <ItemStyle Width="430px" />

    </asp:BoundColumn>

    <asp:BoundColumn DataField="EventDate" HeaderText="Date" >

    <ItemStyle Width="120px" />

    </asp:BoundColumn>

    <asp:ButtonColumn CommandName="Edit" HeaderText="Edit" Text="Edit" >

    <ItemStyle Width="50px" />

    </asp:ButtonColumn>

    <asp:ButtonColumn CommandName="Delete" HeaderText="Delete" Text="Delete" >

    <ItemStyle Width="50px" />

    </asp:ButtonColumn>

    </Columns>

    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

    <EditItemStyle BackColor="#999999" />

    <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />

    <AlternatingItemStyle BackColor="White" ForeColor="#284775" />

    <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />

    </asp:datagrid><asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/Uploads/Northwind.mdb"

    DeleteCommand="DELETE FROM [Events] WHERE [EventID] = ?" InsertCommand="INSERT INTO [Events] ([Notes], [EventID], [EventDate]) VALUES (?, ?, ?)"

    SelectCommand="SELECT [Notes], [EventID], [EventDate] FROM [Events]" UpdateCommand="UPDATE [Events] SET [Notes] = ?, [EventDate] = ? WHERE [EventID] = ?">

    <DeleteParameters>

    <asp:Parameter Name="EventID" Type="Int32" />

    </DeleteParameters>

    <UpdateParameters>

    <asp:Parameter Name="Notes" Type="String" />

    <asp:Parameter Name="EventDate" Type="String" />

    <asp:Parameter Name="EventID" Type="Int32" />

    </UpdateParameters>

    <InsertParameters>

    <asp:Parameter Name="Notes" Type="String" />

    <asp:Parameter Name="EventID" Type="Int32" />

    <asp:Parameter Name="EventDate" Type="String" />

    </InsertParameters>

    </asp:AccessDataSource>

    <br>

    <CE:Editor id="Editor1" EditorWysiwygModeCss="../example.css" Autoconfigure="Simple" Height="200" runat="server" OnTextChanged="Editor1_TextChanged" ></CE:Editor><br />

    <asp:Button id="btnUpdate" onclick="Submit" Runat="server" Text="Add"></asp:Button>

    <asp:Literal ID="Literal1" Runat="server" />

    <br><br>

    <input type="hidden" name="eventid" runat="server" id="eventid">

    </td>

    <tr>

    </table>

    </form>

    </body>

    </html>

    <script runat="server">

    Sub Page_Load(Source as Object, E as EventArgs)

    If Not Page.IsPostBack Then

    MyDataGrid.VirtualItemCount = "60"

    BindData()

    End If

    End Sub

     

    Sub BindData()

    Dim sql as string = "Select EventID, Notes, EventDate from Events"

    Dim conn As OleDbConnection = CreateConnection()

    Dim objDR as OleDbDataReader

    Dim Cmd as New OleDbCommand(sql, conn)

    objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)

    MyDataGrid.DataSource = objDR

    MyDataGrid.DataBind()

    End Sub

     

    Sub UpdateItem(s As Object, e As DataGridCommandEventArgs )

    Dim conn As OleDbConnection = CreateConnection()

     

    'Check if the CommandName==Delete

    If e.CommandName = "Delete" Then

    Dim com As OleDbCommand = New OleDbCommand("DELETE FROM Events WHERE EventID = @id", conn)

    com.Parameters.Add("id", e.Item.Cells(0).Text)

    com.ExecuteNonQuery()

    conn.Close()

    else If (e.CommandName = "Edit") then

    Dim com As OleDbCommand = New OleDbCommand("SELECT Notes FROM Events WHERE EventID = @id", conn)

    com.Parameters.Add("id", e.Item.Cells(0).Text)

    Dim result As OleDbDataReader = com.ExecuteReader()

    If result.Read() Then

    'set the editor text

    Editor1.Text = result.GetString(0)

    eventid.Value = e.Item.Cells(0).Text

    btnUpdate.Text="Update"

    Else

    Editor1.Text = ""

    eventid.Value = ""

    btnUpdate.Text="Add"

    End If

    result.Close()

    End If

    BindData

    End Sub

     

    Sub Submit(s As Object, e As System.EventArgs )

    Dim conn As OleDbConnection = CreateConnection()

    Dim com As OleDbCommand = Nothing

    If Not eventid.Value = String.Empty Then

    com = New OleDbCommand("UPDATE Events SET EventDate = Date(), Notes = @content WHERE EventID = @id", conn)

    com.Parameters.Add("content", Editor1.Text)

    com.Parameters.Add("id", Convert.ToInt32(eventid.Value))

    com.ExecuteNonQuery()

    conn.Close()

    Else

    com = New OleDbCommand("INSERT INTO Events (EventDate, Notes) VALUES (Date(), @content)", conn)

    com.Parameters.Add("content", Editor1.Text)

    com.ExecuteNonQuery()

    conn.Close()

    End If

    BindData

    Me.Response.Redirect(Me.Request.Url.PathAndQuery)

    End Sub

     

    Function CreateConnection() As OleDbConnection

    Dim conn As OleDbConnection = New OleDbConnection

    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Context.Server.MapPath("../uploads/Northwind.mdb") + ";"

    conn.Open()

    Return conn

    End Function

    Protected Sub Editor1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Sub myDataGrid_PageChanger(ByVal Source As Object, _

    ByVal E As DataGridPageChangedEventArgs)

    ' Set the CurrentPageIndex before binding the grid

    myDataGrid.CurrentPageIndex = E.NewPageIndex

    BindData()

    End Sub

    </script>

  •  11-13-2006, 12:52 PM 24280 in reply to 24279

    Re: DataGrid paging not working on the DataBase-Example

    Milton,
     
    Here is the modified version of DataGrid example. This example allows paging. Please copy/paste to the existing file and test it first. Then compare with your modified version and find out the problems.
     
    <%@ Page Language="vb"%>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.OleDb" %>
    <%@ Register TagPrefix="cutesoft" TagName="banner" Src="banner.ascx" %>
    <%@ Register TagPrefix="cutesoft" TagName="leftmenu" Src="leftmenu.ascx" %>
    <%@ Register TagPrefix="CE" Namespace="CuteEditor" Assembly="CuteEditor" %>
    <html>
        <head>
      <title>ASP and ASP.NET WYSIWYG Editor - Working with DataGrid</title>
      <link rel="stylesheet" href="../example.css" type="text/css" />
     </head>
     <body>
            <form runat="server">
       <cutesoft:banner id="banner1" runat="server" /> 
       <table border="0" cellpadding="0" cellspacing="0">
        <tr>
         <td width=10 nowrap></td>
         <td valign="top" nowrap id="leftcolumn" width="160">
          <cutesoft:leftmenu id="leftmenu1" runat="server" />    
         </td>
         <td width="20" nowrap></td>
         <td valign="top" width="760">
          <b>Working with DataGrid</b>
          <hr>
          This example show you how easy it can be to integrate the CuteEditor with the DataGrid.
          <br><br>
          <asp:Datagrid runat="server"
          Id="MyDataGrid"       
          AllowPaging="True"
          PageSize="2"
           PagerStyle-Mode="NumericPages"

          cellpadding="3"
          cellspacing="0"
          Headerstyle-BackColor="#eeeeee"
             Headerstyle-Font-Bold="True"
          BackColor="#f5f5f5"
          BorderWidth="1"
          Width=650
          Font-Name="Arial"
          Font-Size="12px"
          BorderColor="#999999"
          AutogenerateColumns="False"       
          OnPageIndexChanged="MyDataGrid_Page"
          OnEditcommand="MyDataGrid_EditCommand"
          OnCancelcommand="MyDataGrid_Cancel"
          OnUpdateCommand="MyDataGrid_UpdateCommand">
          <Columns>
           <asp:EditCommandColumn
            ButtonType="LinkButton"
            UpdateText="Update"
            CancelText="Cancel"
            EditText="Edit"
                     ItemStyle-HorizontalAlign="Center"
               HeaderText="Edit">
           </asp:EditCommandColumn>
           <asp:BoundColumn
            DataField="EmployeeID"
            HeaderText="ID"
            ReadOnly="True">
           </asp:BoundColumn>
           
           <asp:TemplateColumn HeaderText = "Title">
            <ItemTemplate>
             <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
            </ItemTemplate>
            <EditItemTemplate>
             <CE:Editor id = "FirstName_box" EditorWysiwygModeCss="../example.css" TemplateItemList="Bold,Italic,Underline" Height=150 Width=300 ShowBottomBar="false" Text = '<%#DataBinder.Eval(Container.DataItem, "FirstName") %>' runat = "Server" ></CE:Editor>
            </EditItemTemplate>
           </asp:TemplateColumn>
           <asp:TemplateColumn HeaderText = "Title">
            <ItemTemplate>
             <%# DataBinder.Eval(Container.DataItem, "LastName") %>
            </ItemTemplate>
            <EditItemTemplate>
             <CE:Editor id = "LastName_box" EditorWysiwygModeCss="../example.css" TemplateItemList="Bold,Italic,Underline" Height=150 Width=200 ShowBottomBar="false" Text = '<%#DataBinder.Eval(Container.DataItem, "LastName") %>' runat = "Server" ></CE:Editor>
            </EditItemTemplate>
           </asp:TemplateColumn>
           <asp:TemplateColumn HeaderText = "Title">
            <ItemTemplate>
             <%# DataBinder.Eval(Container.DataItem, "Title") %>
            </ItemTemplate>
            <EditItemTemplate>
             <CE:Editor id = "Title_box" EditorWysiwygModeCss="../example.css" TemplateItemList="Bold,Italic,Underline" Height=150 Width=200 ShowBottomBar="false" Text = '<%#DataBinder.Eval(Container.DataItem, "Title") %>' runat = "Server" ></CE:Editor>
            </EditItemTemplate>
           </asp:TemplateColumn>
          </Columns>
         </asp:DataGrid>
         </td>
        <tr>
       </table>   
      </form>
     </body>
    </html>
    <script runat="server">
     Sub Page_Load(Source as Object, E as EventArgs)
      if not Page.IsPostBack then
       BindData
      end if
     End Sub
      
     Sub BindData()
      Dim sql as string = "Select EmployeeID, FirstName, LastName, Title from Employees"
      Dim conn As OleDbConnection = CreateConnection()
      Dim objDR as OleDbDataReader
      Dim Cmd as New OleDbCommand(sql, conn)
            Dim myAdapter As New OleDbDataAdapter(Cmd)
            Dim ds As New DataSet()
            myAdapter.Fill(ds)
            'Set the datagrid's datasource to the DataSet and databind   
            MyDataGrid.DataSource = ds
      MyDataGrid.DataBind()
      conn.close
     End Sub
     
        Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
            MyDataGrid.CurrentPageIndex = e.NewPageIndex
            BindData()
        End Sub
     
     Sub MyDataGrid_EditCommand(s As Object, e As DataGridCommandEventArgs )
      MyDataGrid.EditItemIndex = e.Item.ItemIndex
      BindData
     End Sub
     Sub MyDataGrid_Cancel(Source As Object,   E As DataGridCommandEventArgs)
      MyDataGrid.EditItemIndex = -1
      BindData()
     End Sub
     
     Sub MyDataGrid_UpdateCommand(s As Object, e As DataGridCommandEventArgs )
      Dim conn As OleDbConnection = CreateConnection()
      Dim MyCommand As OleDbCommand
      Dim txtFirstName As CuteEditor.Editor = e.Item.FindControl("FirstName_Box")
      Dim txtLastName As CuteEditor.Editor = e.Item.FindControl("LastName_Box")
      Dim txtTitle As CuteEditor.Editor = e.Item.FindControl("Title_Box")
      Dim strUpdateStmt As String
       strUpdateStmt =" UPDATE Employees SET" & _
       " FirstName =@Fname, LastName =@Lname, Title = @Title " & _
       " WHERE EmployeeID = @EmpID"
      MyCommand = New OleDbCommand(strUpdateStmt, conn)
      MyCommand.Parameters.Add(New OleDbParameter("@Fname", txtFirstName.text))
      MyCommand.Parameters.Add(New OleDbParameter("@Lname", txtLastName.text))
      MyCommand.Parameters.Add(New OleDbParameter("@Title", txtTitle.text))
      MyCommand.Parameters.Add(New OleDbParameter("@EmpID", e.Item.Cells(1).Text ))
      MyCommand.ExecuteNonQuery()
      MyDataGrid.EditItemIndex = -1
       conn.close
      BindData
     End Sub
     
     Function CreateConnection() As OleDbConnection
            Dim myConnection As OleDbConnection = New OleDbConnection
            myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Context.Server.MapPath("../uploads/Northwind.mdb") + ";"
      myConnection.Open()
            Return myConnection
        End Function
    </script>

     

    asp.net Chat http://cutesoft.net/ASP.NET+Chat/default.aspx
    Web Messenger: http://cutesoft.net/Web-Messenger/default.aspx
    asp.net wysiwyg editor: http://cutesoft.net/ASP.NET+WYSIWYG+Editor/default.aspx
    asp wysiwyg html editor: http://cutesoft.net/ASP
    asp.net Image Gallery: http://cutesoft.net/ASP.NET+Image+Gallery/default.aspx
    Live Support: http://cutesoft.net/live-support/default.aspx

View as RSS news feed in XML