虽然它有runat = server但是不能将div ID传递给后面的代码

时间:2014-03-25 23:14:43

标签: asp.net code-behind formview

我在FormView的ItemTemplate中有一个DIV(id = itemButtons)。虽然它有runat = server,但它的ID不能传递给后面的代码。我收到一个错误,即未声明itemButtons。有什么帮助吗?

代码背后

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" />
            &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
            &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" />
</div>
</ItemTemplate>
</asp:FormView>

1 个答案:

答案 0 :(得分:0)

无法访问它,因为它位于ItemTemplate的{​​{1}}中。只能通过Formiew直接访问位于页面顶部或更好的控件NamigContainer Page

您需要Id使用FindControl NamingContainerFormView需要处于ReadOnly模式,因为它位于ItemTemplate {1}}。

代码的好地方是DataBound - 事件:

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

但是,如果您需要在服务器端访问它,为什么不使用呈现为div的Panel