Frustated of FindControl. FindControl in GridView's ItemTemplate

Last post 06-11-2008 10:46 PM by someguy198650. 22 replies.

Sort Posts:

  • Frustated of FindControl. FindControl in GridView's ItemTemplate

    06-10-2006, 12:37 PM
    • Participant
      1,534 point Participant
    • iloveny
    • Member since 04-08-2006, 5:58 PM
    • Posts 336
    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.

  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    06-10-2006, 1:16 PM
    • Star
      8,703 point Star
    • LudovicoVan
    • Member since 12-02-2004, 3:01 PM
    • The World's End
    • Posts 1,747

    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

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    06-10-2006, 1:22 PM
    • All-Star
      46,022 point All-Star
    • joteke
    • Member since 06-16-2002, 3:24 PM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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.

    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    06-10-2006, 1:30 PM
    • Participant
      1,534 point Participant
    • iloveny
    • Member since 04-08-2006, 5:58 PM
    • Posts 336
    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.

    thanks for your quick reply though.
  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    06-10-2006, 1:36 PM
    • Participant
      1,534 point Participant
    • iloveny
    • Member since 04-08-2006, 5:58 PM
    • Posts 336
    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.

    thanks !!!!

  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    06-10-2006, 1:55 PM
    • All-Star
      46,022 point All-Star
    • joteke
    • Member since 06-16-2002, 3:24 PM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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

    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    06-10-2006, 4:39 PM
    • Participant
      1,534 point Participant
    • iloveny
    • Member since 04-08-2006, 5:58 PM
    • Posts 336
    It takes more than I have to solve this.

    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!

  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    06-11-2006, 3:41 AM
    • All-Star
      46,022 point All-Star
    • joteke
    • Member since 06-16-2002, 3:24 PM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    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

    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    06-11-2006, 10:39 AM
    • Participant
      1,534 point Participant
    • iloveny
    • Member since 04-08-2006, 5:58 PM
    • Posts 336
    JOTEKE, thanks youuuu sooooo mucchhhh for your help. That's exactly what I need. Thanks...!!!
  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    06-11-2006, 12:41 PM
    • Participant
      1,534 point Participant
    • iloveny
    • Member since 04-08-2006, 5:58 PM
    • Posts 336
    just want to drop a message to thank you for your help again.
    I have already finished the project with modifying a little of your code. thanks JOTEKE!

  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    11-08-2006, 5:02 AM
    • Member
      30 point Member
    • gal12
    • Member since 11-08-2006, 9:57 AM
    • Posts 6

    hello..

    I tried to use this function as well.. but i got an error stating that String cannot be converted to int type on this line
      Dim rowindex As Integer = CInt(e.CommandArgument) "

    I tried to convert to String and int16 ,int 32 type but to avail...

    I need some help..

    Thanks in advance..

  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    11-10-2006, 10:18 AM
    • Participant
      1,534 point Participant
    • iloveny
    • Member since 04-08-2006, 5:58 PM
    • Posts 336
    Can you paste the whole code?
  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    11-10-2006, 11:21 AM
    • Member
      219 point Member
    • HotFrost
    • Member since 10-31-2006, 6:22 PM
    • Posts 56

    Thanks God i found this thread...yet i have some problems to implement the code in it..May be someone, can help me out..

    Here is the thing. I am trying to have 'InsertNewRecord' row in the footer of DataGrid:

    i have <Footer Template> where i have text boxes for entering a new record. For example - one of the columns:

    <asp:TemplateField HeaderText="Name">

     <FooterTemplate>

       <asp:TextBox ID="add_Name" Runat="Server" />

     </FooterTemplate>

     <ItemTemplate>

       <%

    #Eval("Name")%>

     </ItemTemplate>

     <EditItemTemplate>

       <asp:TextBox ID="Name" Width="100%" Text='<%#Bind("Name") %>' Runat="server" />

     </EditItemTemplate>

    </asp:TemplateField>

    as you can guess, my edit button is also located in the <footer template> but in the last column.

    Now. For Insert Command  i have this Sub:

    Protected

    Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)

    Dim row As GridViewRow = GridView1.Rows(0)

    (correct me if i am wrong, but 'row' variable will point to footer row..am i wrong?

     

    Nevertheless, i put the code i found in this thread:

    Dim rowindex As Integer = CInt(e.CommandArgument)

    Dim row_to_insert As GridViewRow = GridView1.Rows(rowindex)

    Dim TempControl As Control = CType(row_to_insert_index.FindControl("add_Name"), Control)

    Name = TempControl.ID

     

    it gives me an error:

    'system.FormatException: Input string was not in a correct format.' for Dim rowindex As Ingeger = CInt (e.CommandArgument)

    What may be a problem? Thank you for any help and suggestions!!

  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    11-10-2006, 12:01 PM
    • Member
      30 point Member
    • gal12
    • Member since 11-08-2006, 9:57 AM
    • Posts 6
    thanks.. i managed to solve my problem.. i missed out one part of the code... opps....
  • Re: Frustated of FindControl. FindControl in GridView's ItemTemplate

    11-10-2006, 12:37 PM
    • Member
      219 point Member
    • HotFrost
    • Member since 10-31-2006, 6:22 PM
    • Posts 56

    i think i got where i made an error:

    i need to write:

    Dim row_to_insert as GridViewRow  = GridView1.FooterRow..:):):) - there is no index for footer - just footer row..

    But i still can't get those textboxes in FooterRow to give me their values to make a record insertion!!!

    Dim mytextbox As TextBox = GridView1.FooterRow.FindControl("add_Name")

    Name = mytextbox.Text

     

    What am i doing wrong? Thanks!

Page 1 of 2 (23 items) 1 2 Next >