•      Powered by
 

User Controls, Code-behind & passing values
using custom properties in VB.NET

This simple example demonstrates how to use a User Control and Code Behinds to pass values using Custom Properties to the code-behind form.

ASPX page

<%@ Page Language="VB"%>
<%@ Register TagPrefix="Prefix" TagName="Test" src="uc_test.ascx"%>
<HTML>
          <HEAD>
               <TITLE></TITLE>
         </HEAD>
     <BODY>
             <Prefix:Test CustomProp="No" CustomProp2="Yes" id=Test1 runat="server" />
    </BODY>
</HTML>

 

User Control (uc_test.ascx)

<%@ Control Language="VB" Inherits="uc_test" SRC="uc_test.ascx.vb"%>
Code-behind form (uc_test.ascx.vb

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class uc_test : Inherits System.Web.UI.UserControl
   

    Dim _CustomProp as string
    Dim _CustomProp2 as string


    Public Property CustomProp() As String
    Get
    Return _CustomProp
    End Get
    Set
_CustomProp = value
    End Set
    End Property

    Public Property CustomProp2() As String
    Get
    Return _CustomProp2
    End Get
    Set
_CustomProp2 = value
    End Set
    End Property


    private Sub Page_Load(Sender as object, e as System.EventArgs)

        If CustomProp = "Yes" Then
            Response.Write ("<BR>The value of my custom property (CustomProp) is: " & _CustomProp)
        Else       
'            GetDr( Request.UrlReferrer.ToString())
            Response.Write ("<BR>The value of my custom property (CustomProp2) is: " & _CustomProp2)
        End If

    End Sub

    Private Function GetDr(sqlText as string)
            sqlText = "INSERT INTO TABLES(columns)values('" & sqlText & "')"
            dim sqlConn as SqlConnection  = new SqlConnection(ConnectionString())
            Dim sqlCmd as SqlCommand = new SqlCommand(sqlText,sqlConn)
            sqlCmd.Connection.Open()
            sqlCmd.ExecuteNonQuery()
            sqlCmd.Connection.Close()
    End Function

    Private Function ConnectionString() as string
            dim myConn as string = ConfigurationSettings.AppSettings("DSN")
            return myConn
    End Function

    Private Sub Page_Init(Sender as object, e as System.EventArgs )

    End Sub                    
End Class

Web.Config

<configuration>
    <appSettings>
        <add key="DSN_NorthWind" value="server=(local)\NetSDK;database=NorthWind;Trusted_Connection=yes" />
        <add key="DSN_pubs" value="server=(local)\NetSDK;database=pubs;Trusted_Connection=yes" />
    </appSettings>
</configuration>

 

tio

Terms of Use | Privacy Statement ©2005-2006 IISLogs.com. All rights reserved - Powered by IIS7 - info @ www.IIS.net