cannot find control within control

  • Thread starter Thread starter Daves
  • Start date Start date
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");
 
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!)
 
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");
 
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");

 
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");


 
Back
Top