|
Creating User-Defined Properties, User Controls & Code-Behinds
This example shows how to use a UserControl and a Code-behind to pass values using user-defined
properties.
Here is the code:
The webpage. Test.aspx
<%@ Page Language="C#"%>
<%@ Register TagPrefix="Prefix" TagName="Test"
src="uc_test.ascx"%>
<HTML>
<HEAD></HEAD>
<BODY>
<Prefix:Test CustomProp="No" CustomProp2="Yes" id=Test1
runat="server" />
</BODY>
</HTML> |
The User-Control page uc_test.ascx
| <%@ Control Language="c#" Inherits="uc_test"
SRC="uc_test.ascx.cs"%> |
Code-Behind - uc_test.ascx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class uc_test : System.Web.UI.UserControl
{
private string _CustomProp;
private string _CustomProp2;
public string CustomProp
{
get { return
_CustomProp;}
set { _CustomProp =
value;}
}
public string CustomProp2
{
get { return
_CustomProp2;}
set { _CustomProp2 =
value;}
}
private void Page_Load(object sender, System.EventArgs e)
{
if (CustomProp == "Yes")
{
Response.Write ("<BR>The value of my custom property (CustomProp) is: " +
_CustomProp);
}
else
{
//Uncomment
to test out the inserting the referrer to a database
//string
sqlText = Request.UrlReferrer.ToSting();
//GetDr( sqlText );
Response.Write ("<BR>The value of my custom property (CustomProp2) is: " +
_CustomProp2);
}
} //end Page_Load
private void GetDr( string sqlText)
{
// sqlText =
"INSERT INTO TABLES(columns)values('" + sqlText + "')";
// SqlConnection sqlConn
= new SqlConnection(ConnectionString());
// SqlCommand sqlCmd =
new SqlCommand(sqlText,sqlConn);
//
sqlCmd.Connection.Open();
//
sqlCmd.ExecuteNonQuery();
//
sqlCmd.Connection.Close();
}
private string ConnectionString()
{
return
ConfigurationSettings.AppSettings["DSN"];
}
private void Page_Init( object Sender, System.EventArgs e)
{
}//end Page_Init
} //End Class |
Web.Config holding application settings
<configuration>
<appSettings>
<add key="DSN"
value="server=(local\NetSdk);database=db;Trusted_Connection=yes" />
<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> |
|
|