Print C# properties in aspx,ascx page from code behind file.


Hello all

Today I will show you how to print something in aspx or ascx page from code behind file.In this example I have taken a “Web User Control” named “WebUserControl.ascx”. What I wanted to do was, print a value of a variable in “.ascx” page where the variable has been initialize from “.ascx.cs” better to say code behind file.

To do that we have taken a property in “WebUserControl.ascx.cs” code behind file named “PrintValue” and make it a public property and assign a string value on PageLoad event as bellow :

using System;

public partial class WebUserControl : System.Web.UI.UserControl
{
public string PrintValue { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
PrintValue = "Postback";
else
PrintValue = "Not postback";
}
}

Now I have printed the value of the property in my “WebUserControl.ascx” page as bellow.One thing we have to remember that the script tag should be like : <%= %> to print something in it.

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

The page has - <%= PrintValue %>

Now we can use the control in our page to show the state of the page.
Thats all for today.
Bye

User ScrumPad for your Agile based projects.
,

Leave a Reply