cannot find control within control

D

Daves

hi,
sorry for re-posting but it's a simple question and ought to have a very
simple solution; I am trying to use Findcontrol to grab a Button control
within a Formview control:

<form ID="Form1" runat="server">
<asp:FormView ID="FormView1" Runat="server">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Breyta" CommandName="Edit"/>
...

This is what won't work:
Button Button1 = (Button) FormView1.FindControl("Button1");
 
K

Karl Seguin

Being too lazy to load up VWD, try using:
FormView1.Row.FindControl("Button1")

In a repeater, you'd have multiple "Button1"'s and would need to look at
repeater.Items.FindControl

FormViews are the same..though there's only 1 row...so it's exposed as Row
(if I remember correctly).

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
D

Daves

thx Karl, tried that and bloody error "Object reference not set to an
instance of an object."
Button Button1 = (Button) FormView1.Row.FindControl("Button1");


Karl Seguin said:
Being too lazy to load up VWD, try using:
FormView1.Row.FindControl("Button1")

In a repeater, you'd have multiple "Button1"'s and would need to look at
repeater.Items.FindControl

FormViews are the same..though there's only 1 row...so it's exposed as Row
(if I remember correctly).

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Daves said:
hi,
sorry for re-posting but it's a simple question and ought to have a very
simple solution; I am trying to use Findcontrol to grab a Button control
within a Formview control:

<form ID="Form1" runat="server">
<asp:FormView ID="FormView1" Runat="server">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Breyta" CommandName="Edit"/>
...

This is what won't work:
Button Button1 = (Button) FormView1.FindControl("Button1");
 
K

Karl Seguin

i started it up and it worked fine for me...as you sure you are calling
DataBind on the FormView? Are you sure the datasource has an entry in it?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Daves said:
thx Karl, tried that and bloody error "Object reference not set to an
instance of an object."
Button Button1 = (Button) FormView1.Row.FindControl("Button1");


Karl Seguin said:
Being too lazy to load up VWD, try using:
FormView1.Row.FindControl("Button1")

In a repeater, you'd have multiple "Button1"'s and would need to look at
repeater.Items.FindControl

FormViews are the same..though there's only 1 row...so it's exposed as Row
(if I remember correctly).

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Daves said:
hi,
sorry for re-posting but it's a simple question and ought to have a very
simple solution; I am trying to use Findcontrol to grab a Button control
within a Formview control:

<form ID="Form1" runat="server">
<asp:FormView ID="FormView1" Runat="server">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Breyta" CommandName="Edit"/>
...

This is what won't work:
Button Button1 = (Button) FormView1.FindControl("Button1");

 
D

Daves

Databind() did it...
which I still don't understand! There was an entry in it without doing
databind, could it be that Databind is done later in the event flow?

Karl Seguin said:
i started it up and it worked fine for me...as you sure you are calling
DataBind on the FormView? Are you sure the datasource has an entry in it?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Daves said:
thx Karl, tried that and bloody error "Object reference not set to an
instance of an object."
Button Button1 = (Button) FormView1.Row.FindControl("Button1");


Karl Seguin said:
Being too lazy to load up VWD, try using:
FormView1.Row.FindControl("Button1")

In a repeater, you'd have multiple "Button1"'s and would need to look
at
repeater.Items.FindControl

FormViews are the same..though there's only 1 row...so it's exposed as Row
(if I remember correctly).

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
hi,
sorry for re-posting but it's a simple question and ought to have a very
simple solution; I am trying to use Findcontrol to grab a Button control
within a Formview control:

<form ID="Form1" runat="server">
<asp:FormView ID="FormView1" Runat="server">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Breyta"
CommandName="Edit"/>
...

This is what won't work:
Button Button1 = (Button) FormView1.FindControl("Button1");


 
Joined
Sep 10, 2008
Messages
2
Reaction score
0
Soution to formview findcontrol problem

None of the above suggestions worked for me.
What did work was when I moved the call to the DataBound event.
In VB..

Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
Dim txtUserID As TextBox = FormView1.FindControl("user_idTextBox")
txtUserID.Text = 34

-topherzee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top