Click to learn how to rotate images dynamically with the ASP.NET Image Control
Home
Step by Step ASP.NET Design Solutions
Home
The Ultimate Website Design Guide Using ASP.NET and Adobe Photoshop

Search Engine Submission

 

They Said It Best

Computers are magnificent tools for the realization of our dreams, but no machine can replace the human spark of spirit, compassion, love, and understanding.
Louis Gerstner

 

Show Famous Quotes ASP.NET User Control

ASP.NET User Controls are reusable controls that use the same techniques you would for creating ASP.NET Web pages. You can embed them in ASP.NET pages where they act as a module.

In Step 1. SQL Server table and stored procedure we created a SQL Server table called quotes and a stored procedure called sp_dailyquotes_select to query the data in the quotes table.

In this lesson we are creating an ASP.NET page called quotes.aspx and a ASP.NET user control called quotes.ascx. We'll create a ASP.NET Label to store the famous quote content. We'll edit the code-behind for the User Control and query the database with our stored procedure we created in Step 1.

An obvious question would be "Why not just put the code to query the database in the code-behind for the quotes.aspx page?". The simple answer is that by using the User Control we are creating modle that we can reference on multiple asp.net pages. If we put the code to query the database in the quotes.apx page it would only be visible to that page.

This lesson has the following steps:

Step 2. ASP.NET User Control

Using Visual Studio create a new user control called quotes.ascx.

Edit the new User Control and create a ASP.NET Label called DailyQuotesLabel. The new User Control should look similar to this:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="quotes.ascx.cs" Inherits="controls_dailyquotes" %>

<asp:Label ID="DailyQuotesLabel" runat=server />

Next edit the code-behind for the User Control called quotes.ascx.cs. In the code-behind we create the database connection, execute the stored procedure and store the results the ASP.NET Label DailyQuotesLabel.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class controls_dailyquotes : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    string strResults = "";
    string strConnection = ConfigurationSettings.AppSettings["DBConnect"];

    SqlConnection objConnection = new SqlConnection(strConnection);
    objConnection.Open();

    SqlCommand objCommand = new SqlCommand("sp_dailyquotes_select", objConnection);
    objCommand.CommandType = CommandType.StoredProcedure;

    SqlDataReader objReader = objCommand.ExecuteReader();
 

    while (objReader.Read() == true)
        strResults += objReader["quote"] + "<br><b>" + objReader["author"] + "</b>";

    DailyQuotesLabel.Text = strResults;

    objReader.Close();
    objConnection.Close();
    }
}

Next open the quotes.aspx page and register the new User Control quote.ascx we created as QuotesUC and then reference the control within the body of your page. Your page should look something like this:

<%@ Page Language="C#" CodeFile="quotes.aspx.cs" Inherits="quotes" Title="Quotes" %>
<%@ Register TagPrefix="QuotesUC" TagName="MyQuotes" Src="quotes.ascx" %>

<h2>They said it best:</h2>
<QuotesUC:MyQuotes ID="TheQuotes" runat=server />

Test Drive

Now that everything is seutp correctly you can try it out here quotes.aspx. Each time you refresh the page you will see a new quote.

Privacy Policy Copyright © 2010 dotnetdesignguide.com  Webmaster  Sponsored by www.portable-computer-keyboard.com