User Controls, Code-behind and passing values using custom properties

Contributed by
Rating: 3 stars3 stars3 stars3 stars3 stars / 8
September 16, 2001
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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>
blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials