Home

ASP.Net Web User Control Basics

Blog Date  1 June 2021

The Code

The Holding Page

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="HoldingPage.aspx.cs" Inherits="HoldingPage" %>

<%@ Register Src="~/WebUserControl.ascx" TagPrefix="uc1" TagName="WebUserControl" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <title>Holding Page</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

    <uc1:WebUserControl runat="server" ID="WebUserControl" />

    <hr />

    <asp:Button ID="GetInfoFromControl" OnClick="GetInfoFromControl_Click" Text="Get Info From Control" runat="server" />

    <br />

    <asp:Literal ID="MyShow" runat="server" />

</asp:Content>

The Holding Page Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class HoldingPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GetInfoFromControl_Click(object sender, EventArgs e)
    {
        MyShow.Text = "The User Name is " + WebUserControl.ControlUserName + " and the Password is " + WebUserControl.ControlPassword;
    }
}

The Web Control Page

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

<asp:TextBox ID="UserName" runat="server" /> User Name

<br />

<asp:TextBox ID="PassWord" TextMode="Password" runat="server" />

The Web Control Code Behind

using System;

public partial class WebUserControl : System.Web.UI.UserControl
{
    public string ControlUserName
    {
        get { return UserName.Text; }
        set { UserName.Text = value; }
    }

    public string ControlPassword
    {
        get { return PassWord.Text; }
        set { PassWord.Text = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

The Explanation

This is the absolute billy-bog-basic starter for ten here, nothing complex. 

The idea of user created web controls is to hopefully stop having to create the same form (or part of a form) time and time and time again. As an example your app may require a product information form, once for a new product, once to edit the product, another to display the product. Or perhaps all your forms need the user to enter their ID. I don't know, it's your code.

Above we have a control for the common user name and password information. 

We start with what I have called the Holding Page. This is a regular ASPX page but within that we have placed the control WebUserControl. By declaring public strings within the control and tying them to the textboxes in the control we can get or set the text within the user control.

This control could be added and altered and accessed through countless other pages without having to constantly re-write the username and password textboxes.

As I said, merely a starter for ten.


Need a part time coder? Quite used to working alone and remotely? Contact ren@techsolus.co.uk

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog