ASP 3.5 Formview + Findcontrol in VB - Please help as I'm going crazy
-
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 -
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 AndyIn 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. -
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 Andyur 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
-
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
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
-
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
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.