Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. ASP 3.5 Formview + Findcontrol in VB - Please help as I'm going crazy

ASP 3.5 Formview + Findcontrol in VB - Please help as I'm going crazy

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    breakoutfoo
    wrote on last edited by
    #1

    Hi, I must have searched every forum on the internet and tried every custom function going to try gain control over textboxes in the insert template of my formview using VB code behind without any success and its starting to bug me. Consider the following scenario: If I have a formview with ID: "Details" and a textbox called: "CampaignTextBox" in the and wish to set the .text value of "CampaignTextBox" to "Hello World" I believe the most obvious method is as follows: Dim Dynamic_Text As TextBox = CType(Me.Details.FindControl("CampaignTextBox"), TextBox) Dynamic_Text.text = "Hello World" It doesn't matter how I manipulate this code I cannot get my code to find the control - I get a null reference exception - "Object reference not set to an instance of an object.." Other details about my page that may affect this (possibly): 1-It is a page that uses a master page, does this cause issues? 2-If my formview is within a div would this make any difference? Could anyone give me any clues or if at all possible an example using the parameters given above and save me from going bald before I get to 30? Thanks Andy

    B D 2 Replies Last reply
    0
    • B breakoutfoo

      Hi, I must have searched every forum on the internet and tried every custom function going to try gain control over textboxes in the insert template of my formview using VB code behind without any success and its starting to bug me. Consider the following scenario: If I have a formview with ID: "Details" and a textbox called: "CampaignTextBox" in the and wish to set the .text value of "CampaignTextBox" to "Hello World" I believe the most obvious method is as follows: Dim Dynamic_Text As TextBox = CType(Me.Details.FindControl("CampaignTextBox"), TextBox) Dynamic_Text.text = "Hello World" It doesn't matter how I manipulate this code I cannot get my code to find the control - I get a null reference exception - "Object reference not set to an instance of an object.." Other details about my page that may affect this (possibly): 1-It is a page that uses a master page, does this cause issues? 2-If my formview is within a div would this make any difference? Could anyone give me any clues or if at all possible an example using the parameters given above and save me from going bald before I get to 30? Thanks Andy

      B Offline
      B Offline
      breakoutfoo
      wrote on last edited by
      #2

      In case anyone suggest these, I have also just tried omitting "Me" Dim Dynamic_Text As TextBox = CType(Details.FindControl("CampaignTextBox"), TextBox) and trying to add the extra level of "row" Dim Dynamic_Text As TextBox = CType(Details.row.FindControl("CampaignTextBox"), TextBox) and experience the same problem.

      1 Reply Last reply
      0
      • B breakoutfoo

        Hi, I must have searched every forum on the internet and tried every custom function going to try gain control over textboxes in the insert template of my formview using VB code behind without any success and its starting to bug me. Consider the following scenario: If I have a formview with ID: "Details" and a textbox called: "CampaignTextBox" in the and wish to set the .text value of "CampaignTextBox" to "Hello World" I believe the most obvious method is as follows: Dim Dynamic_Text As TextBox = CType(Me.Details.FindControl("CampaignTextBox"), TextBox) Dynamic_Text.text = "Hello World" It doesn't matter how I manipulate this code I cannot get my code to find the control - I get a null reference exception - "Object reference not set to an instance of an object.." Other details about my page that may affect this (possibly): 1-It is a page that uses a master page, does this cause issues? 2-If my formview is within a div would this make any difference? Could anyone give me any clues or if at all possible an example using the parameters given above and save me from going bald before I get to 30? Thanks Andy

        D Offline
        D Offline
        dotnetmember
        wrote on last edited by
        #3

        ur coding is correct but u need to make dummy binding to form view to make it build control hierarchy. try this in page load, If Not Page.IsPostBack Then Dim arr As New ArrayList() arr.Add(1) Details.DataSource = arr Details.DataBind() End If

        B 1 Reply Last reply
        0
        • D dotnetmember

          ur coding is correct but u need to make dummy binding to form view to make it build control hierarchy. try this in page load, If Not Page.IsPostBack Then Dim arr As New ArrayList() arr.Add(1) Details.DataSource = arr Details.DataBind() End If

          B Offline
          B Offline
          breakoutfoo
          wrote on last edited by
          #4

          Hi, thanks for your reply. When I try the code in the page load event I get the following error: "Both DataSource and DataSourceID are defined on 'Details'. Remove one definition" What could cause this error message in this context? I don't understand the need to create a dummy binding. Would you mind explaining as if I understand it I may be able to figure out where I'm going wrong? Cheers Andy

          B 1 Reply Last reply
          0
          • B breakoutfoo

            Hi, thanks for your reply. When I try the code in the page load event I get the following error: "Both DataSource and DataSourceID are defined on 'Details'. Remove one definition" What could cause this error message in this context? I don't understand the need to create a dummy binding. Would you mind explaining as if I understand it I may be able to figure out where I'm going wrong? Cheers Andy

            B Offline
            B Offline
            breakoutfoo
            wrote on last edited by
            #5

            I have finally cracked it. No dummy databinds required or anything other than the findcontrol method. The key is purely down to using the method at the correct time in the page life cycle. I used the following to set a textbox to the current date and time. where: Formview ID = "Details" Formview child Textbox to change ID = "Date_AddedTextBox" (Textbox is in the insert template) Protected Sub Details_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles Details.DataBound If Details.CurrentMode = FormViewMode.Insert Then Dim Test As TextBox = CType(Details.Row.FindControl("Date_AddedTextBox"), TextBox) Test.Text = Now() End If End Sub So ... if like me you have been to hell and back via the entire internet trying to do what seems like the simplest thing, trust your code as it is probably correct and try it at a time where the items have definitely been created and and data definitely bound (such as the event I used above) and you should have success.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups