Hi hi!

Just that, I want to hide a asp:button when a asp:dataview is empty and show it when it has records...

Any Ideas?

Omar

Dani AI

Generated

Note for : since your control is a GridView and it’s bound with an ObjectDataSource, the safest place to show/hide the button is after the bind has completed. is right to check the number of rows, but put that check in an event that runs after the GridView has received its data (rather than relying on Page_Load).

A simple, reliable approach is the GridView.DataBound event. Example:

Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.DataBound
    btnAction.Visible = (GridView1.Rows.Count > 0)
End Sub
protected void GridView1_DataBound(object sender, EventArgs e)
{
    btnAction.Visible = GridView1.Rows.Count > 0;
}

If you prefer to inspect exactly what the ObjectDataSource returned (handy when the Select method returns a DataTable or a collection), use the ObjectDataSource.Selected event and check e.ReturnValue:

Protected Sub ObjectDataSource1_Selected(ByVal sender As Object, ByVal e As ObjectDataSourceStatusEventArgs) Handles ObjectDataSource1.Selected
    Dim rv = e.ReturnValue
    Dim hasRows As Boolean = False
    If TypeOf rv Is Data.DataTable Then
        hasRows = DirectCast(rv, Data.DataTable).Rows.Count > 0
    ElseIf TypeOf rv Is System.Collections.IEnumerable Then
        Dim en = DirectCast(rv, System.Collections.IEnumerable).GetEnumerator()
        hasRows = en.MoveNext()
    End If
    btnAction.Visible = hasRows
End Sub

Notes and cautions:

  • If your GridView uses paging, Rows.Count reflects the current page only. To decide based on the total number of records, return a total count from your data layer or use the data source’s count method.
  • Use Visible = False server-side to avoid rendering the button HTML when there’s no data. If you need client-side toggling, hide with CSS/JS instead.
  • EmptyDataTemplate in the GridView lets you show a “no records” message without extra code; combine it with one of the above checks to control other controls.

Recommended Answers

All 3 Replies

for Dataview use

If dataview.count = 0 then
button1.visible = False
else
button1.visible = True
End If

or
for datatables use

If datatable.rows.count = 0 then 
button1.visible = False
else
button1.visible = True
End If

Thanks for your reply ptaylor965, !!

Well, first of all, I made a mistake about the component, Its a gridview; second of all, on which event do I put these codes? consider that my gridview its binded by design with a objectdatasource, and I can't change that.

Thanks again!!

Omar

for Dataview use

If dataview.count = 0 then
button1.visible = False
else
button1.visible = True
End If

or
for datatables use

If datatable.rows.count = 0 then 
button1.visible = False
else
button1.visible = True
End If

For DataGrids use

If datagrid.rows.count = 0 then 
button1.visible = False
else
button1.visible = True
End If

I would place this code on form_load and on any buttons or events that fill/refresh the datagrid

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.