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>| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP.NET Articles More By aspfree developerWorks - FREE Tools! | Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
| | | | Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
| | | | Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
| | | | Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available. FREE! Go There Now!
| | | | Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months. FREE! Go There Now!
| | | | Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications. FREE! Go There Now!
| | | | Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
| | | | Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
| | | | The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |