Export not working

3 posts, 0 answers
  1. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 03 Jul 2012 Link to this post

    I have a grid that I bind in the code behind. Whenever I click the export button, it just removes the paging and displays all 6000 records on one page, and doesn't export anything. Any ideas why? Here's the aspx and code behind 

    <%@ Page Title="" Language="VB" MasterPageFile="~/Main.master" AutoEventWireup="false"
        CodeFile="CustomerList.aspx.vb" Inherits="Customers_CustomerList" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <asp:Content ID="Content2" ContentPlaceHolderID="mainContentPlaceHolder" Runat="Server">
        <telerik:RadFormDecorator ID="decorator" Skin="Default" DecoratedControls="Buttons"
            runat="server" />
                    <telerik:RadTextBox ID="searchInput" runat="server">
                    <asp:Button ID="searchGrid" runat="server" Text="Search" />
                    <asp:Button ID="Reset" runat="server" Text="reset" />
        <br />
    <telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" AllowPaging="True"
            AllowSorting="True" AutoGenerateColumns="false" PageSize="50" Height="400px">
            <MasterTableView DataKeyNames="customerid" AutoGenerateColumns="false">
                    <HeaderStyle Width="20px"></HeaderStyle>
                    <HeaderStyle Width="20px"></HeaderStyle>
                    <telerik:GridBoundColumn AllowSorting="false" DataField="Edit" HeaderText="<span style='visibility: hidden;'>a</span>">
                    <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName"
                    <telerik:GridBoundColumn DataField="LastName" HeaderText="LastName" SortExpression="LastName"
                    <telerik:GridBoundColumn DataField="Business Name" HeaderText="Business Name" SortExpression="Business Name"
                        UniqueName="Business Name">
                    <telerik:GridBoundColumn DataField="State" HeaderText="State" SortExpression="State"
                    <telerik:GridBoundColumn DataField="Zip" HeaderText="Zip" SortExpression="Zip" UniqueName="Zip">
                    <telerik:GridBoundColumn DataField="Address" HeaderText="Address" SortExpression="Address"
                    <telerik:GridBoundColumn DataField="City" HeaderText="City" SortExpression="City"
                    <telerik:GridBoundColumn DataField="Phone" HeaderText="Phone" SortExpression="Phone"
                    <telerik:GridBoundColumn DataField="Fax" HeaderText="Fax" SortExpression="Fax" UniqueName="Fax">
                    <telerik:GridBoundColumn DataField="Email" HeaderText="Email" SortExpression="Email"
                    <telerik:GridBoundColumn DataField="CellPhone" HeaderText="CellPhone" SortExpression="CellPhone"
            <HeaderStyle Width="100px" />
                    <Scrolling AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="True">
        <asp:Label ID="output" runat="server"></asp:Label>
        <br />
        <asp:Button ID="export" Text="Export Data to Excel" runat="server" />

    Imports System.Data
    Imports System.Data.Sql
    Imports System.Data.SqlClient
    Imports Telerik.Web.UI
    Partial Class internal_Clientlist
        Inherits System.Web.UI.Page
        Dim conn = System.Configuration.ConfigurationManager.ConnectionStrings("connection").ToString
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                colDrop.DataSource = getColumns()
                colDrop.DataValueField = "colName"
                colDrop.DataTextField = "name"
                Session("searchDrop") = ""
                'check the callback date
            End If
        End Sub 'page Load
        'this sub gets the data for the grid
        Protected Friend Sub bindGrid()
                Dim mytable As New DataTable
                mytable.Columns("userid").DataType = System.Type.GetType("System.Int16")
                mytable.Columns("NextContactDate").DataType = System.Type.GetType("System.DateTime")
                mytable.Columns("Rating").DataType = System.Type.GetType("System.Int16")
                mytable.Columns("InterestLevel").DataType = System.Type.GetType("System.Int16")
                Dim username = Membership.GetUser.UserName
                'if the search button is visible then we have to get the different sql
                Dim gridSql
                If (search.Visible) Then
                    gridSql = "SELECT tblUsers.UserId AS uid, FirstName, LastName, SchoolName, State, Interest, Rating, CONVERT(VARCHAR(10), NextContactDate, 101) AS NextContactDate, " & _
                    "InterestLevel FROM tblUsers WHERE isDeleted='no' "
                End If
                Using myconn As New SqlConnection(conn)
                    Dim reader
                    'if this is a salesman, we have to find the states they get.
                    If (Roles.IsUserInRole(Membership.GetUser.UserName, "salesman")) Then
                        reader = New SqlCommand("SELECT Name FROM userstates INNER JOIN states ON statefk = statesid WHERE username = '" & username & "'", myconn).ExecuteReader()
                        If (reader.HasRows()) Then
                            gridSql &= "AND( "
                            While reader.Read()
                                gridSql &= "State='" & reader("Name") & "' OR "
                            End While
                            gridSql = gridSql.Substring(0, (gridSql.Length - 4)) & ") "
                            gridSql = "SELECT tblUsers.UserId AS uid, FirstName, LastName, Email FROM tblUsers WHERE isDeleted='no' AND State='blahblahblah' "
                        End If 'if reader.hasrows
                    End If 'if isuserinrole
                    gridSql &= "ORDER BY tblUsers.UserId"
                    reader = New SqlCommand(gridSql, myconn).ExecuteReader
                    While reader.Read()
                        mytable.Rows.Add(reader("uid"), reader("FirstName"), reader("LastName"), reader("SchoolName"), reader("State"), reader("Interest"), _
                                         reader("Rating"), reader("NextContactDate"), reader("InterestLevel"))
                    End While
                End Using 'using myconn
                'Dim dataSource As New SqlDataSource
                'dataSource.ConnectionString = conn
                'dataSource.SelectCommand = gridSql
                clientGrid.DataSource = mytable
            Catch ex As Exception
                output.Text = "<div class=""outputDiv"" style=""color: red;"">There was an error updating the customer. Please contact the systme administrator if the problem persists.</div>"
            End Try
        End Sub 'bindGrid
        Protected Sub clientGrid_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles clientGrid.Init
            Dim menu As GridFilterMenu = clientGrid.FilterMenu
            Dim i = 0
            While i < menu.Items.Count
                If (menu.Items(i).Text = "NoFilter") Then
                    menu.Items(i).Text = "Clear"
                ElseIf menu.Items(i).Text = "Contains" Then
                    menu.Items(i).Text = "Search"
                    menu.Items(i).Visible = False
                End If
                i = i + 1
            End While 'while i <
        End Sub 'rad grid init
        Protected Sub clientGrid_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles clientGrid.ItemCommand
            If (e.CommandName = "RowClick" AndAlso TypeOf e.Item Is GridDataItem) Then
                Dim id = e.Item.Cells(2).Text
                Response.Redirect("EditClient.aspx?id=" & id)
            End If
        End Sub 'itemcommand
        Protected Sub export_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles export.Click
            Dim mydate = Date.Now
            Dim fileName = mydate.Year & "-" & mydate.Month & "-" & mydate.Day & " - Client List"
            clientGrid.ExportSettings.OpenInNewWindow = True
            clientGrid.ExportSettings.ExportOnlyData = True
            clientGrid.ExportSettings.IgnorePaging = True
            'clientGrid.ExportSettings.FileName = fileName
        End Sub 'export click
        'this function returns the data table for the column dropdown
        Protected Friend Function getColumns() As DataTable
            Dim myTable As New DataTable
            myTable.Rows.Add("- Please Select -", "- Please Select -", "none")
            myTable.Rows.Add("Address", "Address", "text")
            myTable.Rows.Add("CallAt1Text", "Call Attempt 1 Status", "drop")
            myTable.Rows.Add("CallAt2Text", "Call Attempt 2 Status", "drop")
            myTable.Rows.Add("CallAt3Text", "Call Attempt 3 Status", "drop")
            myTable.Rows.Add("CallAt4Text", "Call Attempt 4 Status", "drop")
            myTable.Rows.Add("checkedLiterature", "Checked Literature", "check")
            myTable.Rows.Add("City", "City", "text")
            myTable.Rows.Add("ContactedDate", "Contacted Date", "date")
            myTable.Rows.Add("DateLiteratureChecked", "Date Literature Checked", "date")
            myTable.Rows.Add("DateRegistered", "Date Registered", "date")
            myTable.Rows.Add("Email", "Email Address", "text")
            myTable.Rows.Add("ZipCode", "Zip", "text")
            myTable.Rows.Add("ExtendedZipCode", "Extended Zip Code", "text")
            myTable.Rows.Add("Fax", "Fax", "text")
            myTable.Rows.Add("Interest", "Interest", "drop")
            myTable.Rows.Add("InterestLevel", "Interest Level", "drop")
            myTable.Rows.Add("NextContactDate", "Callback Date", "date")
            myTable.Rows.Add("NextContactTime", "Callback Time", "time")
            myTable.Rows.Add("Notes", "Notes", "text")
            myTable.Rows.Add("PhoneNumber", "Phone Number", "text")
            myTable.Rows.Add("SalesNotes", "Sales Notes", "text")
            myTable.Rows.Add("Title", "Title", "text")
            myTable.Rows.Add("UserID", "User Id", "text")
            myTable.Rows.Add("userPosition", "Position", "drop")
            Return myTable
        End Function 'getColums
        Protected Sub clientGrid_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles clientGrid.NeedDataSource
        End Sub 'need data source
        'find what they picked and then set the corresponding thing to visible
        Protected Sub colDrop_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles colDrop.SelectedIndexChanged
            search.Visible = True
            'set all these to invisible initially
            searchCheck.Visible = False
            searchDate.Visible = False
            searchDrop.Visible = False
            searchInput.Visible = False
            searchTime.Visible = False
            Dim myTable = getColumns()
            Dim type
            For Each i In myTable.Rows
                If (i("colName") = colDrop.SelectedValue) Then
                    type = i("type")
                    Exit For
                End If
            Next 'for each
            'now show the proper field
            If (type = "text") Then
                searchInput.Visible = True
            ElseIf (type = "drop") Then
                searchDrop.Visible = True
                'only bind if this is a new dropdown item
                If (Not Session("searchDrop") Is Nothing AndAlso Session("searchDrop") <> colDrop.SelectedValue) Then
                    Dim searchTable As New DataTable
                    'get all of the data from the database to create the dropdown
                    Using myconn As New SqlConnection(conn)
                        Dim reader = New SqlCommand("SELECT value FROM SearchDropItems WHERE colName ='" & colDrop.SelectedValue & "'", myconn).ExecuteReader()
                        While reader.Read()
                            searchTable.Rows.Add(reader(0), reader(0))
                        End While
                    End Using
                    searchDrop.DataSource = searchTable
                    searchDrop.DataTextField = "value"
                    searchDrop.DataValueField = "value"
                End If 'if not
            ElseIf (type = "time") Then
                searchTime.Visible = True
            ElseIf (type = "date") Then
                searchDate.Visible = True
            ElseIf (type = "check") Then
                searchCheck.Visible = True
            ElseIf (type = "none") Then
                search.Visible = False
                searchCheck.Visible = False
                searchDate.Visible = False
                searchDrop.Visible = False
                searchInput.Visible = False
                searchTime.Visible = False
            End If
                Session("searchDrop") = colDrop.SelectedValue
        End Sub 'colDrop index changed
        'if they clicked search
        Protected Sub search_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles search.Click
            'bind the grid
        End Sub 'search click
        'this sub finds the proper search values
        Protected Friend Sub findSearchValue(ByRef sql As String)
            sql = "SELECT tblUsers.UserId AS uid, FirstName, LastName, SchoolName, State, Interest, Rating, CONVERT(VARCHAR(10), NextContactDate, 101) AS NextContactDate, " & _
                "InterestLevel FROM tblUsers WHERE isDeleted='no' "
            'see what type we are looking for
            If (searchInput.Visible) Then
                sql &= "AND " & colDrop.SelectedValue & " like '%" & searchInput.Text & "%' "
            ElseIf (searchCheck.Visible) Then
                sql &= "AND " & colDrop.SelectedValue & " = '" & searchCheck.Checked & "' "
            ElseIf (searchDate.Visible) Then
                sql &= "AND " & colDrop.SelectedValue & " = '" & searchDate.SelectedDate & "' "
            ElseIf (searchDrop.Visible) Then
                sql &= "AND " & colDrop.SelectedValue & " = '" & searchDrop.SelectedValue & "' "
            ElseIf (searchTime.Visible) Then
                sql &= "AND " & colDrop.SelectedValue & " = '" & searchTime.SelectedDate & "' "
            End If
        End Sub 'findSearchValue
    End Class

  2. Daniel
    Daniel avatar
    4949 posts

    Posted 06 Jul 2012 Link to this post


    Please examine the following link:
    Export from Ajaxified Grid

    I would also recommend that you take advantage of the advanced data-binding:
    Advanced Data-binding (using NeedDataSource event)

    Best regards,
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. Web Services
    Web Services avatar
    243 posts
    Member since:
    Apr 2008

    Posted 06 Jul 2012 Link to this post

    I added


    to my page load and it's working. Thanks for the help.

Back to Top