Ok, I am sick, frustated and stressed out of FindControl in GridView already.
I have already tried search this forum, google, yahoo, and you name it. Nothing works for me. Or maybe it's just me that don't have knowledge?
I have 2 colums GridView. One is Name as BoundField. This column is binded to Name field from my SqlDataSource control.
The second one is TemplateField. In this TemplateField, I have one button and one HiddenField.
I bind the HiddenField to NameID field from my SqlDataSource.
Now outside of this GridView, I have label
What I am trying to do is when user click the button in the gridview, the Label will show NameID. My Button click event is
Dim HiddenField as HiddenField = ctype(GridView1.FindControl("HiddenField"), HiddenField)
Label.Text = HiddenField.Value.
However, when I click the Button, it gives me error:
Object reference not set to an instance of an object.
How can I find the control in the gridview?
I always read in the forum that FindControl only works at NamingContainer. What is NamingContainer?
I really appreciate your reply and thanks for trying to get the rock out of my brain. It's really killing me.
GridView is a control which repeats its rows based on the bound data, e.g in practise it creates a set of controls (one row in it) as many times as there are rows in data source. In order to keep IDs of these controls unique, GridView and its rows are naming
containers (implement INamingContainer interface). Being a naming container means that a control provides a new naming scope for its child controls by appending it's ID into ID of its own naming container.
And being a naming container means that in order to look for a control with its local ID (not the unique id) you need to run FindControl against its naming container. In this case you run it againt GridView but GridView is
not the direct naming container (or parent) of the HiddenField but
the corresponding row (GridViewRow) is.
So, assuming you want the HiddenField located on the first row of GridView, you'd use
Dim HiddenField as HiddenField = ctype(GridView1.Rows(0).FindControl("HiddenField"), HiddenField)
Label.Text = HiddenField.Value
E,g to put it simple, you need to tell from which row you want to access the control, as it can be repeated multiple times.Just running FindControl againts GridView doesn't specify, which of them you want to access.
I have come accross that link you just gave me too.
However, that wasn't what I really need to do.
The link that you gave me has one button which is placed at the bottom of the GridView. As for what I am trying to do is to put the button in EVERY row. Then when clicked it will populate Label.text with the HiddenField.Value.
Also, if I apply that code, the link that you gave me, it will give me different result.
What I am trying to do is when user click on the button, the LABEL.text will show the NameID of the PARTICULAR ROW where the button is clicked.
But when I apply that code, the Label.text will always be NameID of the last record. This happens because the 'For Each... In...' syntax. The 'For...Each... In...' syntax will go each row and get the NameID until the last row.
JOTEKE, you just have a surgery for my brain and take that rock out of my f** head. This is GREATT !!! It works fine. PLUS, I have more understanding of NAMING CONTAINER that alwasy kick my brain from inside.
I think now I will need to find a way to create a variable that represents each row so when the button is clicked, it will show the NameID of the Particular clicked row.
I give a hint that you can place use a ButtonField in GridView. With ButtonField you can handle GridView's RowCommand event, where you get index of the current row via command argument. Then you could locate the row being clicked and get the corresponding
controls with FindControl
Okay, yes, I use ButtonField in my GridView.
Now, how can I get the index of the current row? I try GridView1.SelectedIndex but it always return -1. And as long as I know is that, ButtonField can't fire any evnet, or may I don't see where I can set this. Obviously, there is no event I can set when I am
in property of ButtonField, unless I convert it to TemplateField.
Plus, when ButtonField is clicked, it won't select the row. The only thing that can SELECT the row is when the Select Command added to the table.
Also, where do I put this code? Under GridView_RowCommand?
As I said earlier, you handle GridView's RowCommand event, just set CommandName for the ButtonField, and it will bubble GridView to raise it's RowCommand event. In RowCommand event, you can get the index of the row via CommandArgument supplied by the event
argument. Here's a sample:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "myRowSelect" Then
Dim rowindex As Integer = CInt(e.CommandArgument)
Dim row As GridViewRow = GridView1.Rows(rowindex)
Dim lbl As Label = DirectCast(row.FindControl("lblSample"), Label)
Response.Write("You clicked row having Label with text: " & lbl.Text)
End If
End Sub
Of course, if you put CommandName to be Select, that would raise GridView's SelectedIndexChanging/:Selected events automatically,, where you could also do determining of the row, and again look controls on the row
iloveny
Participant
1534 Points
336 Posts
Frustated of FindControl. FindControl in GridView's ItemTemplate
Jun 10, 2006 04:37 PM|LINK
I have already tried search this forum, google, yahoo, and you name it. Nothing works for me. Or maybe it's just me that don't have knowledge?
I have 2 colums GridView. One is Name as BoundField. This column is binded to Name field from my SqlDataSource control.
The second one is TemplateField. In this TemplateField, I have one button and one HiddenField.
I bind the HiddenField to NameID field from my SqlDataSource.
Now outside of this GridView, I have label
What I am trying to do is when user click the button in the gridview, the Label will show NameID. My Button click event is
Dim HiddenField as HiddenField = ctype(GridView1.FindControl("HiddenField"), HiddenField)
Label.Text = HiddenField.Value.
However, when I click the Button, it gives me error:
Object reference not set to an instance of an object.
How can I find the control in the gridview?I always read in the forum that FindControl only works at NamingContainer. What is NamingContainer?
I really appreciate your reply and thanks for trying to get the rock out of my brain. It's really killing me.
LudovicoVan
Star
9682 Points
1935 Posts
Re: Frustated of FindControl. FindControl in GridView's ItemTemplate
Jun 10, 2006 05:16 PM|LINK
Hello iloveny,
the fact is in a gridview the container is the single row...
Here is a starting point: http://www.codeproject.com/dotnet/AccessingControlsInsideGr.asp
HTH. -LV
joteke
All-Star
46224 Points
6896 Posts
ASPInsiders
MVP
Re: Frustated of FindControl. FindControl in GridView's ItemTemplate
Jun 10, 2006 05:22 PM|LINK
Hi,
GridView is a control which repeats its rows based on the bound data, e.g in practise it creates a set of controls (one row in it) as many times as there are rows in data source. In order to keep IDs of these controls unique, GridView and its rows are naming containers (implement INamingContainer interface). Being a naming container means that a control provides a new naming scope for its child controls by appending it's ID into ID of its own naming container.
And being a naming container means that in order to look for a control with its local ID (not the unique id) you need to run FindControl against its naming container. In this case you run it againt GridView but GridView is not the direct naming container (or parent) of the HiddenField but the corresponding row (GridViewRow) is.
So, assuming you want the HiddenField located on the first row of GridView, you'd use
Dim HiddenField as HiddenField = ctype(GridView1.Rows(0).FindControl("HiddenField"), HiddenField)
Label.Text = HiddenField.Value
E,g to put it simple, you need to tell from which row you want to access the control, as it can be repeated multiple times.Just running FindControl againts GridView doesn't specify, which of them you want to access.
Teemu Keiski
Finland, EU
iloveny
Participant
1534 Points
336 Posts
Re: Frustated of FindControl. FindControl in GridView's ItemTemplate
Jun 10, 2006 05:30 PM|LINK
However, that wasn't what I really need to do.
The link that you gave me has one button which is placed at the bottom of the GridView. As for what I am trying to do is to put the button in EVERY row. Then when clicked it will populate Label.text with the HiddenField.Value.
Also, if I apply that code, the link that you gave me, it will give me different result.
What I am trying to do is when user click on the button, the LABEL.text will show the NameID of the PARTICULAR ROW where the button is clicked.
But when I apply that code, the Label.text will always be NameID of the last record. This happens because the 'For Each... In...' syntax. The 'For...Each... In...' syntax will go each row and get the NameID until the last row.
thanks for your quick reply though.
iloveny
Participant
1534 Points
336 Posts
Re: Frustated of FindControl. FindControl in GridView's ItemTemplate
Jun 10, 2006 05:36 PM|LINK
I think now I will need to find a way to create a variable that represents each row so when the button is clicked, it will show the NameID of the Particular clicked row.
thanks !!!!
joteke
All-Star
46224 Points
6896 Posts
ASPInsiders
MVP
Re: Frustated of FindControl. FindControl in GridView's ItemTemplate
Jun 10, 2006 05:55 PM|LINK
Hi,
I give a hint that you can place use a ButtonField in GridView. With ButtonField you can handle GridView's RowCommand event, where you get index of the current row via command argument. Then you could locate the row being clicked and get the corresponding controls with FindControl
Teemu Keiski
Finland, EU
iloveny
Participant
1534 Points
336 Posts
Re: Frustated of FindControl. FindControl in GridView's ItemTemplate
Jun 10, 2006 08:39 PM|LINK
Okay, yes, I use ButtonField in my GridView.
Now, how can I get the index of the current row? I try GridView1.SelectedIndex but it always return -1. And as long as I know is that, ButtonField can't fire any evnet, or may I don't see where I can set this. Obviously, there is no event I can set when I am in property of ButtonField, unless I convert it to TemplateField.
Plus, when ButtonField is clicked, it won't select the row. The only thing that can SELECT the row is when the Select Command added to the table.
Also, where do I put this code? Under GridView_RowCommand?
Thanks JOTEKE, for your guide and time!
joteke
All-Star
46224 Points
6896 Posts
ASPInsiders
MVP
Re: Frustated of FindControl. FindControl in GridView's ItemTemplate
Jun 11, 2006 07:41 AM|LINK
As I said earlier, you handle GridView's RowCommand event, just set CommandName for the ButtonField, and it will bubble GridView to raise it's RowCommand event. In RowCommand event, you can get the index of the row via CommandArgument supplied by the event argument. Here's a sample:
You have GridView:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:ButtonField Text="Try me" CommandName="myRowSelect" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblSample" runat="server" Text='<%#String.Format("Label on row {0}",Ctype(Container,GridViewRow).RowIndex + 1)%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And relevant code
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "myRowSelect" Then
Dim rowindex As Integer = CInt(e.CommandArgument)
Dim row As GridViewRow = GridView1.Rows(rowindex)
Dim lbl As Label = DirectCast(row.FindControl("lblSample"), Label)
Response.Write("You clicked row having Label with text: " & lbl.Text)
End If
End Sub
Of course, if you put CommandName to be Select, that would raise GridView's SelectedIndexChanging/:Selected events automatically,, where you could also do determining of the row, and again look controls on the row
Teemu Keiski
Finland, EU
iloveny
Participant
1534 Points
336 Posts
Re: Frustated of FindControl. FindControl in GridView's ItemTemplate
Jun 11, 2006 02:39 PM|LINK
iloveny
Participant
1534 Points
336 Posts
Re: Frustated of FindControl. FindControl in GridView's ItemTemplate
Jun 11, 2006 04:41 PM|LINK
I have already finished the project with modifying a little of your code. thanks JOTEKE!