Domanda

This code seems simple enough on the surface but I am trying to display a message when no records are present in datalist.

I have this on markup:

<asp:DataList ID="DataList1" runat="server" CellPadding="4"
   DataSourceID="SqlDataSource1"
   Font-Bold="False" OnSelected="SqlDataSource1_Selected" Font-Names="Verdana"
   Font-Size="Small" RepeatColumns="2"
   RepeatDirection="Horizontal" Width="100%" ForeColor="#333333">
   <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
...
...
</asp:DataList>
<asp:label CssClass="Treb10Blue" ID="lblMsg" runat="server"></asp:Label> 

Then on codebehind, I have this:

Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected

  If e.AffectedRows = 0 Then
      lblMsg.Visible = True
      lblMsg.Text = "No records found"
  Else
      lblMsg.Text = ""
  End If

End Sub

I am not getting any errors but the message is not displaying.

Any ideas what could be wrong?

È stato utile?

Soluzione

This issue has been resolved. A much easier solution than I would have imagined.

   <FooterTemplate>
     <asp:Label forecolor="#9ACD32" Visible='<%# IIF(DataList1.Items.Count=0 And ddlLocation.SelectedItem.Value<>"0", "True", "False")%>' runat="server" ID="lblMsg" Text="No records found"></asp:Label>
    </FooterTemplate>

Altri suggerimenti

You probably want to check the number of items in your DataList. Try this:

Protected Sub SqlDataSource1_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource1.Selected

  If DataList1.Items.Count = 0 Then
      lblMsg.Visible = True
      lblMsg.Text = "No records found"
  Else
      lblMsg.Text = ""
  End If

End Sub

This is an excerpt from MSDN, maybe it's what you're seeing:

All operations return the number of rows affected by the operation. The AffectedRows property has the same value as the return value of the Update, Insert, and Delete methods.

When the Select method is called and the data source is set to DataReader mode, the return value is 0 in all cases.

If that's the case, then maybe you can reverse your logic:

  • by default, display the label with the 'No records found'
  • create a handler for the the DataList's ItemDataBound event, and hide the label there (because in that case you have at least one record)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top