This is a migrated thread and some comments may be shown as answers.

problem trying to display "No records to display" on page load

2 Answers 214 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chom
Top achievements
Rank 1
Chom asked on 22 Feb 2010, 06:50 AM
Hello

I am having problems trying to display the message (or a custom message) "No records to diplay" in the radgrid when my page first loads.

I have tried putting in this code on the page load event:

 

If Not IsPostBack Then

 

rgListingWithImages.DataSource =

New Object() {}

 

 

'rgListingWithImages.Rebind()

 

 

End If

 

 
but I get this error.
Both DataSource and DataSourceID are defined on 'rgListingWithImages'.  Remove one definition.

Here is my radgrid code:
***************************************************************************************************************************************************************

<

 

telerik:RadGrid ID="rgListingWithImages" runat="server" GridLines="None" Skin="Office2007"

 

 

PageSize="8" ClientSettings-Scrolling-SaveScrollPosition="true" AllowPaging="True"

 

 

DataSourceID="dsBusinessListings">

 

 

<HeaderStyle HorizontalAlign="Left" />

 

 

<ItemStyle HorizontalAlign="Left" />

 

 

<AlternatingItemStyle HorizontalAlign="Left" />

 

 

<ClientSettings>

 

 

</ClientSettings>

 

 

<PagerStyle Mode="NumericPages" />

 

 

<MasterTableView PageSize="5" DataSourceID="dsBusinessListings" AutoGenerateColumns="False"

 

 

DataKeyNames="ID" CommandItemDisplay="Top" ShowHeadersWhenNoRecords="true" NoMasterRecordsText="Insert some records here">

 

 

<Columns>

 

 

<telerik:GridButtonColumn CommandName="DeleteRow" CommandArgument="DeleteRow" HeaderText=""

 

 

Text="Delete" UniqueName="DeleteRow">

 

 

</telerik:GridButtonColumn>

 

 

<telerik:GridBoundColumn DataField="ID" DefaultInsertValue="" HeaderText="ID" SortExpression="ID"

 

 

UniqueName="ID" DataType="System.Int32" ReadOnly="True" Visible="false">

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn DataField="CustomerID" DefaultInsertValue="" HeaderText="CustomerID"

 

 

SortExpression="CustomerID" UniqueName="CustomerID" Visible="false">

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn DataField="OrderID" DefaultInsertValue="" HeaderText="OrderID"

 

 

SortExpression="OrderID" UniqueName="OrderID">

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn DataField="OrderDate" DataType="System.DateTime" DefaultInsertValue=""

 

 

HeaderText="OrderDate" SortExpression="OrderDate" UniqueName="OrderDate" Visible="false">

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn DataField="BusinessListingNumber" DataType="System.Int32"

 

 

DefaultInsertValue="" HeaderText="BusinessListingNumber" SortExpression="BusinessListingNumber"

 

 

UniqueName="BusinessListingNumber">

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn DataField="NumberOfImages" DataType="System.Int32" DefaultInsertValue=""

 

 

HeaderText="NumberOfImages" SortExpression="NumberOfImages" UniqueName="NumberOfImages">

 

 

</telerik:GridBoundColumn>

 

 

<telerik:GridBoundColumn DataField="BusinessListingDescription" DefaultInsertValue=""

 

 

HeaderText="BusinessListingDescription" SortExpression="BusinessListingDescription"

 

 

UniqueName="BusinessListingDescription">

 

 

</telerik:GridBoundColumn>

 

 

</Columns>

 

 

<PagerStyle Mode="NextPrev" />

 

 

<CommandItemTemplate>

 

 

<asp:Button ID="btnInsertNewRecord" runat="server" CommandName="InsertNewRecord"

 

 

Text="New Record" Width="90px" Height="22px" />

 

 

</CommandItemTemplate>

 

 

</MasterTableView>

 

 

</telerik:RadGrid>

 

 

<asp:SqlDataSource ID="dsBusinessListings" runat="server" ConnectionString="<%$ ConnectionStrings:hbocdbConnectionString %>"

 

 

SelectCommand="admin_Customers_BusinessListing_SelectPrimaryInfo" SelectCommandType="StoredProcedure"

 

 

InsertCommand="admin_Customers_BusinessListing_InsertPrimaryInfo" InsertCommandType="StoredProcedure"

 

 

DeleteCommand="admin_Customers_BusinessListing_DeleteRowPrimaryInfo" DeleteCommandType="StoredProcedure">

 

 

<SelectParameters>

 

 

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

 

 

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

 

 

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

 

 

</SelectParameters>

 

 

<InsertParameters>

 

 

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

 

 

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

 

 

<asp:Parameter Name="TabStripSelectedIndex" Type="Int16" />

 

 

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

 

 

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

 

 

</InsertParameters>

 

 

<DeleteParameters>

 

 

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

 

 

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

 

 

<asp:Parameter Name="BusinessListingNumber" Type="Int16" />

 

 

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

 

 

</DeleteParameters>

 

 

</asp:SqlDataSource>

 

 

<asp:Button ID="Button1" runat="server" Text="Button" />
***************************************************************************************************************************************************************

and my code behind to follow:
***************************************************************************************************************************************************************

 

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 

 

'Temp for querystring params

 

 

'Set up the viewstate vars

 

 

Me.CustomerID = "BLC-7SUZE1SNNF-242010"

 

 

Me.OrderID = "OID-KWUV6-2212010"

 

 

Me.BusinessListingNumber = 1

 

 

Me.WithSlideNoSlide = "WS"

 

 

If Not IsPostBack Then

 

rgListingWithImages.DataSource =

New Object() {}

 

 

'rgListingWithImages.Rebind()

 

 

End If

 

 

End Sub

 

 

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

 

dsBusinessListings.SelectParameters(

"CustomerID").DefaultValue = "BLC-7SUZE1SNNF-242010"

 

dsBusinessListings.SelectParameters(

"OrderID").DefaultValue = "OID-KWUV6-2212010"

 

dsBusinessListings.SelectParameters(

"WithSlideNoSlide").DefaultValue = "WS"

 

 

End Sub

 

 

#Region

 

"RadGrid With Images Events."

 

 

Protected Sub rgListingWithImages_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgListingWithImages.ItemCommand

 

 

'Check which command name was selected

 

 

Select Case e.CommandName

 

 

Case "InsertNewRecord"

 

 

'Insert a new row into the BusinessListings table

 

dsBusinessListings.InsertParameters(

"CustomerID").DefaultValue = Me.CustomerID

 

dsBusinessListings.InsertParameters(

"OrderID").DefaultValue = Me.OrderID

 

dsBusinessListings.InsertParameters(

"TabStripSelectedIndex").DefaultValue = 0

 

dsBusinessListings.InsertParameters(

"BusinessListingDescription").DefaultValue = "Business Listing with slides."

 

dsBusinessListings.InsertParameters(

"WithSlideNoSlide").DefaultValue = "WS"

 

 

'Call the Insert method

 

dsBusinessListings.Insert()

 

'Refresh the grid data

 

SelectgridWithImages()

 

Exit Select

 

 

Case "DeleteRow"

 

 

'Delete the selected row from the BusinessListing table

 

 

Dim myItem As GridDataItem = DirectCast(e.Item, GridDataItem)

 

 

Dim CustID As TableCell = myItem("CustomerID")

 

 

Dim OrdID As TableCell = myItem("OrderID")

 

 

Dim ListNo As TableCell = myItem("BusinessListingNumber")

 

 

''Fill delete parameters

 

dsBusinessListings.DeleteParameters(

"CustomerID").DefaultValue = CustID.Text

 

dsBusinessListings.DeleteParameters(

"OrderID").DefaultValue = OrdID.Text

 

dsBusinessListings.DeleteParameters(

"BusinessListingNumber").DefaultValue = CType(ListNo.Text, Integer)

 

dsBusinessListings.DeleteParameters(

"WithSlideNoSlide").DefaultValue = "WS"

 

 

'Call delete method

 

dsBusinessListings.Delete()

 

'Refresh the grid data

 

SelectgridWithImages()

 

Exit Select

 

 

End Select

 

 

End Sub

 

 

Protected Sub rgListingWithImages_PageIndexChanged(ByVal source As Object, ByVal e As Telerik.Web.UI.GridPageChangedEventArgs) Handles rgListingWithImages.PageIndexChanged

 

SelectgridWithImages()

 

End Sub

 

 

Private Sub SelectgridWithImages()

 

 

'This is a basic select statement to fill the grid with data

 

dsBusinessListings.SelectParameters(

"CustomerID").DefaultValue = "BLC-7SUZE1SNNF-242010"

 

dsBusinessListings.SelectParameters(

"OrderID").DefaultValue = "OID-KWUV6-2212010"

 

dsBusinessListings.SelectParameters(

"WithSlideNoSlide").DefaultValue = "WS"

 

 

End Sub

 

#End

 

Region
***************************************************************************************************************************************************************

I have tried several things including the needdatasource but it still is not comming up.
Can someone point me in the right direction.

Thanks

 

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 Feb 2010, 08:11 AM
Hi,

Try the setting the Datasource of the grid in the NeedDataSource event as shown  below. You need to remove the DataSourceID property from the aspx and set it in the code. On a postback such as a button click  you will need to call RadGrid1.Rebind to reset the datasource.

C#
  protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            RadGrid1.DataSource = new string[] { }; 
        } 
        else 
        { 
            RadGrid1.DataSourceID = "SqlDataSource1"
        } 
    } 


Thanks,
Princy


0
Chom
Top achievements
Rank 1
answered on 22 Feb 2010, 04:29 PM
Hi Princy

Thankyou. That did the trick. I still had a problem when I inserted a new record with the radgrid_itemcommand after the page load. The grid disappeared completely. I realized that the grid was not populated si all I did was call my stored proc to populate the grid after inserted a new row.

I appreciate your time. This has been a big help.

Have a good day.
Chommy.
Tags
Grid
Asked by
Chom
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Chom
Top achievements
Rank 1
Share this question
or