Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Gridview in ASP.NET VB and a label - Pretty Noob stuff.

Posted on 2009-05-18
6
Medium Priority
?
416 Views
Last Modified: 2012-06-27
Hi everyone.

I have a stupid question I was hoping someone would help with. I'm tired and just can't think anymore.

I have an website with three drop down lists that are attached to an Access Database.

I have a gridview that holds an SQL Statement to query the database based on the DDL controls.

SO if you select location as Florida in the drop down, the statement reads: "SELECT * FROM table WHERE location = Florida"

Make Sense?

If you hit a button, the page refreshes, the SQL Statement Runs and a gridview of the query results is displayed.

Here's my question.

When that GridView displays I have a hidden label called "lblStatusSearch" that I want to remain blank (and therefore hidden).

BUT when the gridview DOES NOT display (ie the SQL query yields no results I want that label to say "Try Again"

I know the VB code for the label, I'm not quite THAT new (lblStatusSearch.Text = "try again")

I just don't know where to put the IF statement to cause it to appear!

Here's what I got. Let me know if you need more. Thanks
Imports System.Data.SqlClient
 
Partial Public Class frmClient
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
    End Sub
 
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReset.Click
 
        Response.Redirect("~/frmClient.aspx")
 
    End Sub
 
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
 
    End Sub
 
    Protected Sub gvResults_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gvResults.SelectedIndexChanged
 
        If gvResults.Visible Then
            lblStatusSearch.Text = ""
        Else
            lblStatusSearch.Text = "Try Again"
        End If
 
    End Sub
 
    Protected Sub tblUsersGridView_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles tblUsersGridView.Selecting
 
    End Sub
End Class

Open in new window

0
Comment
Question by:IncubusJax
6 Comments
 
LVL 12

Accepted Solution

by:
wht1986 earned 500 total points
ID: 24419068
I would put in the gridview databound event something like

if GridView1.Rows.Count = 0 then
  lblStatusSearch.Visible =  true
else
  lblStatusSearch.Visible =  false
end
0
 
LVL 11

Assisted Solution

by:Muhammad Ousama Ghazali
Muhammad Ousama Ghazali earned 500 total points
ID: 24419199
Instead of using a separate label I'll recommend using the EmptyDataTemplate of the GridView. Use the Source or Design mode in VS to edit this templated as per your requirements, put a label in it, some hyperlink, upto you. But it will automatically appear when there are no records to be displayed after grid is bound.
0
 
LVL 2

Assisted Solution

by:krishrr
krishrr earned 500 total points
ID: 24419226
GridView control has default property 'EmptyDataText'. You can try the following

<asp:GridView id="grid1" runat="server" EmptyDataText="Try Again!" />
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Assisted Solution

by:ddayx10
ddayx10 earned 500 total points
ID: 24419263
Yeah...

I don't even think you have to use the rowdatabound here. As long as you check the gridview.rows.count after databinding has occurred it should give you an accurrate count and you can fill the label as necessary.

As I think about it if you had a label on each line then you woiuld use rowdatabound to evaluate some value in the line, but here we are talking about the database yielding no records at all. I don't even know if rowdatabound will be called in that case.

Since your code doesn't show the databinding process Im guessing your using a sqldatasource? I think that binds in the pre_render event.. So you could just put your code in page load:

        If gvResults.Rows.Count > 0 Then
            lblStatusSearch.Text = ""
        Else
            lblStatusSearch.Text = "Try Again"
        End If

Worst case scenario put it in page_loadcomplete (I dont think that should be necessary).

dday
0
 
LVL 12

Expert Comment

by:wht1986
ID: 24421401
i didnt say use the rowdatabound event, i said put that code in the gridview databound event. It always fires when the gridview goes through the binding cycle.  I like to place the code here because no matter what caused the bind (manual force, sorting, paging) this event will fire. But I agree with the emptyTemplate, if you can,  use that.  I based my response assuming the use of the label was part of your requirements.
0
 

Author Closing Comment

by:IncubusJax
ID: 31582884
Thanks everyone. Actually, you were all correct and led me to my solution. I ended up just using the empty data text property of the gridview but I tested and considered all.

You guys are great!

Thanks
Mark
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question