I have a DIV (id=itemButtons) inside the ItemTemplate of a FormView. Although it has runat=server its ID can¨t be passed to the code behind. I get an error that itemButtons is not declared. Any help?
CODE BEHIND
Sub Page_Load() Handles Me.Load
itemButtons.visible = True
End Sub
ASPX PAGE
<asp:FormView ID="FormView1" runat="server" DataKeyNames="IDrecipe" DataSourceID="SqlDataSource1">
<ItemTemplate>
<div id="itemButtons" visible="false" runat="server">
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" />
</div>
</ItemTemplate>
</asp:FormView>
It is not accessible because it is in the ItemTemplate
of the Formiew
. Only controls which are sitting on top of the page, or better, which NamigContainer
is the Page
can be accessed directly via Id
.
You need to use FindControl
on it's NamingContainer
which is the FormView
that needs to be in the ReadOnly
-mode because it is in the ItemTemplate
.
A good place for the code is the DataBound
- event:
Private Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
Select Case FormView1.CurrentMode
Case FormViewMode.ReadOnly
Dim itemButtons = DirectCast(FormView1.FindControl("itemButtons"), HtmlGenericControl)
itemButtons.isible = True
End Select
End Sub
But why don't you use a Panel
which is rendered as a div if you need to access it on serverside?