Solved

Gridview Empty Data template How to capture data

Posted on 2009-05-09
20
2,786 Views
Last Modified: 2013-11-26
Hi All,

I have a gridview which includes a databound textbox (located in the footer of the grid) which is used to search a database, this control works prefectly using the code below to capture the search text.

Dim ProdName = TryCast(GridView3.FooterRow.FindControl("ProductSearchTB"), TextBox)
        SqlDataSource4.SelectParameters("PROD_DESC").DefaultValue = ProdName.Text

My issue is this, I need to be able to run the same search from an empty Gridview, I originally tried
        Dim ProdName = TryCast(GridView3.EmptyDataTable.FindControl("NewProductSearchTB"), TextBox)
        SqlDataSource4.SelectParameters("PROD_DESC").DefaultValue = ProdName

But get the following error
'EmptyDataTable' is not a member of 'System.Web.UI.WebControls.GridView'.      

Is there another way to capture the search text rather than using FindControl?

Many Thanks
0
Comment
Question by:taylor99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
20 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24343998
How is the GridView defined in HTML?
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24344006
For some reason I don't seem to be able to find the direct way for you (never had this situation myself), but by "interrogating" the parents of the textbox (using an OnLoad event of the textbox and checking the sender object), I found the namingcontainers.

There are no rows available, however, there are apparently GridViewRows hidden somewhere else. There must be an easier way, but this at least works, once you know that the data is empty:

' get the textbox when data is empty '
Dim tb As TextBox  = DirectCast(GridView3.Controls[0].Controls[0].FindControl("newProductSearchTB"), TextBox)

Open in new window

0
 
LVL 10

Expert Comment

by:cdebel
ID: 24344010
The problem is that when your datatable is empty, your footerrow doesn't exist.
Can you show me how you defined your section: <EmptyDataTemplate></EmptyDataTemplate> ?

My guess is that you placed an asp:table named "EmptyDataTable", and inside of it you have this NewProductSearchTB.  If this is the case you need to do this
Dim et as Table
Dim tb as TextBox
et = CType(GridView3.FindControl("EmptyDataTable"), Table)
tb = CType(et.FindControl("NewProductSearchTB"), TextBox)
... then whatever you want with your tb textbox

0
Independent Software Vendors: 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 39

Expert Comment

by:abel
ID: 24344020
Note, the first Controls[0] is of type ChildTable. The second Controls[0] returns a type GridViewRow, which contains your EmptyDataTemplate.

One might be tempted to do something like this:

GridView3.EmptyDataTemplate.FindControl
but that's only the template, not the naming container. As a result, it cannot find your control for you.
0
 
LVL 39

Expert Comment

by:abel
ID: 24344033
Btw, considering the assumed confusion about EmptyDataTable: I assumed you mistyped that and meant EmptyDataTemplate and that you had something like this, which will work with the code I showed:

<asp:GridView ...decl...>
   <.. other controls ..>
 
   <EmptyDataTemplate>
	<asp:TextBox runat="server" 
		text="hello"
		ID="newProductSearchTB"/>
   </EmptyDataTemplate>	
		
</asp:GridView>

Open in new window

0
 
LVL 10

Expert Comment

by:cdebel
ID: 24344040
As abel said, in his solution, the cell must be empty (no other thing than your TexBox).  So if you add a label beside your textbox, make sure that you point to the right control index.  I would rather use a FindControl approach even if you don't like it.

If you don't like this FindControl approach, you could use this new version which find a control even if its in a sub-sub-sub item :)
    Private Function FindControlRecursive(ByVal root As Control, ByVal id As String) As Control
        If root Is Nothing Then
            Return Nothing
        End If
        If root.ID = id Then
            Return root
        End If
        Dim c As Control
        For Each c In root.Controls
            Dim t As Control = FindControlRecursive(c, id)
            If Not t Is Nothing Then
                Return t
            End If
        Next
        Return Nothing
    End Function

Open in new window

0
 
LVL 10

Expert Comment

by:cdebel
ID: 24344046
You can use this FindControlRecursive like this:

Dim tb as Textbox
tb = CType(FindControlRecursive(GridView3, "NewProductSearchTB"), TextBox)
0
 
LVL 39

Expert Comment

by:abel
ID: 24344091
Yes, that's a nice solution. It's quite a well-known technique and it your code is explained here: http://www.devx.com/vb2themax/Tip/19449

> As abel said, in his solution, the cell must be empty (no other thing than your TexBox).

That was not what I meant. You can have hundredths of controls inside the EmptyDateTemplate, in fact, you can have them anywhere. Otherwise, the FindControl in my code would not have been necessary.

If there is data in the grid, the code will simply return Nothing, all you need to check for is the returned value:

If IsNothing(tb) Then
   ...


etc.

In other words: it will return the textbox is there is no data, and it will return nothing if there is data. In short: the returned value is always usable.
0
 
LVL 39

Expert Comment

by:abel
ID: 24344135
One issue not touched on in this thread so far is that of the events. We have seen little of the actual ASPX (nothing, actually), but if you need this TextBox as the result of someone clicking on a button which is located inside the EmptyDataTemplate, there's a very direct way to get to your TextBox, and if you are in this situation, I recommend using this direct technique:




Protected Sub myButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim tb As TextBox = CType(CType(sender, Button) _
        .NamingContainer.FindControl( _
        "newProductSearchTB"), TextBox)
End Sub

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24344196
Looking further into this, there appear to be more and more ways to get this information. Strange that this intel is so scattered about on the internet. So, after:

method #1: using Control[0].Control[0].FindControl (not pretty, but stable)
method #2: using FindControlRecursive (has the danger of finding duplicates)
method #3: using an event of another control inside EmptyDataTemplate
method #4: using GridView_RowDataBound event (see below)

In the GridView_RowDataBound event, it is trivial to check for the row type, and if it is the correct type, you immediately have the correct typed object at your disposal:

    Protected Sub GridView3_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView3.RowDataBound
        If e.Row.RowType = DataControlRowType.EmptyDataRow Then
            Dim tb As TextBox = DirectCast(e.Row.FindControl("newProductSearchTB"), TextBox)
        End If
    End Sub

Open in new window

0
 
LVL 10

Expert Comment

by:cdebel
ID: 24344230
The chances to find a duplicate in an EmptyDataTemplate is as big as messing up with Controls index.

But i think that Taylor got the idea and have enough solutions.  
0
 
LVL 39

Expert Comment

by:abel
ID: 24344298
> The chances to find a duplicate in an EmptyDataTemplate is as big as messing up with Controls index.

agreed, unless in this case the controls-array is fixed for the very first controls that are added due to the design of GridView. But there's a naming-clash possibility if a row contains an item of the same name when the datasource is non-empty.

So, basically, if you want to be on the sure side, you need to use the control's events, or the RowDataBound event (and maybe there are still many other methods?).

Interesting thread, learning something here... ;)
0
 
LVL 39

Expert Comment

by:abel
ID: 24344299
unless == although
0
 
LVL 10

Expert Comment

by:cdebel
ID: 24344311
I think i already tried all theses solutions in the past.  As you said, there's so much methods on the net.  I had my problem in the past trying to figure out what was the best way.

I like to use the RowDataBound method to add some javascript onclick action (to confirm a delete for example)
0
 

Author Closing Comment

by:taylor99
ID: 31579765
Perfect, thanks abel
0
 
LVL 10

Expert Comment

by:cdebel
ID: 24344357
pls abel, next time... tell me to not get into this kind of thread :-).  lol.
0
 
LVL 39

Expert Comment

by:abel
ID: 24344805
@OP: glad we could be of some help :)


> pls abel, next time... tell me to not get into this kind of thread :-).  lol.
why not? Though the question should've been closed with a split points... Why it isn't I don't know.
0
 
LVL 10

Expert Comment

by:cdebel
ID: 24344839
Because obviously, the askers tried the first thing that he saw, even if others took some of their week-end time to answer them.  

I start to think that i should just keep myself over the 3000 points per month and that's it.  Got 46500 points this month, but i'll never get to Guru level with that kind of askers
0
 
LVL 39

Expert Comment

by:abel
ID: 24344902
don't get grumpy, it's a sunny day (well, here it is). If the original asker is still around, (s)he can request a reopening of the question by clicking "request attention". After reopening (s)he can split the points.

@taylor99, to prevent this in the future, please read: http://www.experts-exchange.com/help.jsp?hi=407 on closing questions and this: http://www.experts-exchange.com/help.jsp#hi100 on splitting points
0
 
LVL 10

Expert Comment

by:cdebel
ID: 24344920
There's no need to re-open the question, i'll live with few points less.  

But thanks for giving him links explaining how to split points.
0

Featured Post

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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