简体   繁体   English

如何为用户控制文件编写代码

[英]How to do Code Behind for a User Control File

I have a dynamic menu that shows select items based on flags in my database. 我有一个动态菜单,该菜单显示基于数据库中标志的选择项。 Currently I have the menu on my .aspx page and the code behind on my .aspx.vb page. 目前,我在.aspx页面上具有菜单,而在.aspx.vb页面上具有代码。 I'm adding in alot more pages, so I'm trying to seperate the menu using a User Control. 我要添加更多页面,因此我尝试使用用户控件来分隔菜单。 However, I'm not sure how to reference the code behind for the .ascx page. 但是,我不确定如何引用.ascx页面的代码。

Current code: 当前代码:

<ul>
<li id="form1" runat="server"><a title="a" href="a.aspx" target="_blank">A Form</a></li>
<li id="form2" runat="server"><a title="b" href="b.aspx" target="_blank">B Form</a></li>
</ul>

Code Behind: 背后的代码:

Private Sub Customer_LoadData()
        'DECLARE LOCAL VARIABLES
        Dim objContract As Contract = Nothing
        Dim objContractDL As New ContractDL

        'RETRIEVE THE CUSTOMER'S CONTRACT INFORMATION
        If objContractDL.Read(objContract, zintCustomerID) Then

            Call DisplayCustomer(objContract)
            Call DisplayForms(objContract)

        End If

    End Sub    
Private Sub DisplayForms( _
            ByVal objContract As Contract _
        )

            If (objContract.fieldFedLoansFlag = "0") Then
                authform1.Visible = False           
            Else
                authform1.Visible = True
            End If
End Sub

I've never seen a .ascx.vb page before, so i don't think that is the solution. 我以前从未看过.ascx.vb页面,因此我认为这不是解决方案。

From the comments above you are using the Page Directive and with Custom Controls you should be using the Control Directive 根据上面的注释,您正在使用“页面”指令,而在“自定义控件”中,您应该使用“控件”指令

<%@ Control Language="vb" AutoEventWireup="false" CodeFile="menu2.ascx.vb"
Inherits="menu2" %>

From the Microsoft site the difference between a user control and a page 在Microsoft网站上,用户控件和页面之间的区别

A user controls differs from an ASP.NET Web page in these ways: 用户控件在以下方面与ASP.NET网页不同:

  • The file name extension for the user control is .ascx. 用户控件的文件扩展名是.ascx。
  • Instead of an @ Page directive, the user control contains an @ Control directive that defines configuration and other properties. 用户控件代替@ Page指令,而包含定义配置和其他属性的@ Control指令。
  • User controls cannot run as stand-alone files. 用户控件不能作为独立文件运行。 Instead, you must add them to ASP.NET pages, as you would any control. 相反,您必须像控制任何控件一样将它们添加到ASP.NET页面。
  • The user control does not have html, body, or form elements in it. 用户控件中没有html,body或form元素。 These elements must be in the hosting page. 这些元素必须在托管页面中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM