Accessing Controls nested in a formview

R

RGF

Hi, I am using server controls (textbox, dropdownlist, calendar)
inside of a form view, it seems that the .Net framework (VB.Net & Net
Frame Work 2.0) makes it difficult to access the control properties
when embedded inside of a FormView control.

What I am trying to do is to enable or disable a textbox control
programatically by accessing the property of the control, along the
lines of:

textbox1.Enable = false

However, when the textbox control is embedded in a Formview nested in
the EditItemTemplate then the textbox is Not accessible. I would
think that the following syntax should work, but it does not:


FormView1.EditItemTemplate.Textbox1.Enable = false


Could anyone suggest the proper syntax for me to Get/Set the
properties of controls embedded inside of a formview while using
VB.Net & .Net Frame Work 2.0?

-r
 
R

Riki

RGF said:
Hi, I am using server controls (textbox, dropdownlist, calendar)
inside of a form view, it seems that the .Net framework (VB.Net & Net
Frame Work 2.0) makes it difficult to access the control properties
when embedded inside of a FormView control.

What I am trying to do is to enable or disable a textbox control
programatically by accessing the property of the control, along the
lines of:

textbox1.Enable = false

However, when the textbox control is embedded in a Formview nested in
the EditItemTemplate then the textbox is Not accessible. I would
think that the following syntax should work, but it does not:


FormView1.EditItemTemplate.Textbox1.Enable = false


Could anyone suggest the proper syntax for me to Get/Set the
properties of controls embedded inside of a formview while using
VB.Net & .Net Frame Work 2.0?

Use FindControl:

CType(FormView1.FindControl("TextBox1"),TextBox).Enabled = false;
 
R

RGF

Hi, I am using servercontrols(textbox, dropdownlist, calendar)
inside of a form view, it seems that the .Net framework (VB.Net & Net
Frame Work 2.0) makes it difficult toaccessthe control properties
when embedded inside of aFormViewcontrol.

What I am trying to do is to enable or disable a textbox control
programatically by accessing the property of the control, along the
lines of:

textbox1.Enable = false

However, when the textbox control is embedded in aFormviewnestedin
the EditItemTemplate then the textbox is Not accessible. I would
think that the following syntax should work, but it does not:

FormView1.EditItemTemplate.Textbox1.Enable = false

Could anyone suggest the proper syntax for me to Get/Set the
properties ofcontrolsembedded inside of aformviewwhile using
VB.Net & .Net Frame Work 2.0?

-r

Update:

I am able to "Get" (read) the value - see sample code below, the <
MsgBox(tb.Text) > does retrieves the text value, however the following
SET argument < tb.Enabled = False > does not seem to take effect on
the control, since after the Sub is executed the textbox control
remains enabled - Any thoughts on this side effect? I would think I
should be able to "Set" the properties of the control, right?

Thanks in advanced,
-r


Partial Class Users_Details4
Inherits System.Web.UI.Page

Protected Sub FormView1_ItemUpdated(ByVal sender As Object, ByVal
e
As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles
FormView1.ItemUpdated

Dim tb As TextBox =
DirectCast(FormView1.FindControl("TitleTextTextBox"), TextBox)

Dim Cal As Calendar =
DirectCast(FormView1.FindControl("Calendar1"), Calendar)

'Note: e Cal.SelectedDate = 9/22/2007 and Date.Today = 8/22/2007

If Cal.SelectedDate >= Date.Today Then

MsgBox(tb.Text) 'Display the text found in the control
tb.Enabled = False 'Set the State of the control

End If

End Sub

End Class
 
R

RGF

Use FindControl:

CType(FormView1.FindControl("TextBox1"),TextBox).Enabled = false;

Riki, thanks for the feedback, I tried your suggestion, and it seems
that I am able to read (Get) the text value from the control without
problem, however, I am not able to "SET" the Enable = False, thus the
control remains editable. Could you suggest a solution with regards
to why the SET function is not working? (see code snip below)

Thanks in advanced,
-r



Partial Class Users_Details4
Inherits System.Web.UI.Page

Protected Sub FormView1_ItemUpdated(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles
FormView1.ItemUpdated

'Used the following msg calls to confirm that the return sets
are correct Calendar1 = 9/10/2007 and
'Date.Today = 8/22/2007
MsgBox(CType(FormView1.FindControl("Calendar1"),
Calendar).SelectedDate)
MsgBox(Date.Today)



If (CType(FormView1.FindControl("Calendar1"),
Calendar).SelectedDate) >= Date.Today Then

'Used the following msg call to confirm that the control
to be affected can be read
MsgBox(CType(FormView1.FindControl("MessageTextTextBox"),
TextBox).Text)

'The following arguments sets the state of the control
CType(FormView1.FindControl("MessageTextTextBox"),
TextBox).Enabled = False

'used the following to force the formview to remain in the
edittemplate
e.KeepInEditMode = True
End If


End Sub

End Class
 
R

RGF

Riki, thanks for the feedback, I tried your suggestion, and it seems
that I am able to read (Get) the text value from the control without
problem, however, I am not able to "SET" the Enable = False, thus the
control remains editable. Could you suggest a solution with regards
to why the SET function is not working? (see code snip below)

Thanks in advanced,
-r

Partial Class Users_Details4
Inherits System.Web.UI.Page

Protected Sub FormView1_ItemUpdated(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.FormViewUpdatedEventArgs) Handles
FormView1.ItemUpdated

'Used the following msg calls to confirm that the return sets
are correct Calendar1 = 9/10/2007 and
'Date.Today = 8/22/2007
MsgBox(CType(FormView1.FindControl("Calendar1"),
Calendar).SelectedDate)
MsgBox(Date.Today)

If (CType(FormView1.FindControl("Calendar1"),
Calendar).SelectedDate) >= Date.Today Then

'Used the following msg call to confirm that the control
to be affected can be read
MsgBox(CType(FormView1.FindControl("MessageTextTextBox"),
TextBox).Text)

'The following arguments sets the state of the control
CType(FormView1.FindControl("MessageTextTextBox"),
TextBox).Enabled = False

'used the following to force theformviewto remain in the
edittemplate
e.KeepInEditMode = True
End If

End Sub

End Class

I fixed the issue by adding the code into the FormView Prerender
event, now the controls are set correctly based on the conditional
statements I added.

Thanks for the hint!
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top