google analytics

Thursday, May 28, 2009

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.

Monday, May 4, 2009

File streaming in .NET

Hello All

Today i will focus on simple file steaming in .NET which I have to implement it in one of my project.Though it’s not optimize but its OK for serve small purpose.

In the project I have to read a PDF file from the server and with FileStream and Flash it with Response to the client side.

the code is as bellow.

private void generateRequestedPDF()
{
    try
    {
        string file = Path.GetFileName(Request.QueryString["RefID"]);// get file name from query string.use your custom method." 

        FileStream fileStream = new FileStream(Server.MapPath(@"~\uploadedFiles\" + file), FileMode.Open);
        long fileSize = fileStream.Length;

        byte[] buffer = new byte[(int)fileSize];
        fileStream.Read(buffer, 0, (int)fileSize);
        fileStream.Close();

        Response.Clear();

        Response.ContentType = "Application/pdf";// use your custom type"
        Response.AddHeader("Content-Disposition", "inline; filename=" + file);

        Response.OutputStream.Write(buffer, 0, (int)fileSize);
        Response.Flush();
        Response.Close();
    }
    catch (Exception exc)
    {
    //  throw exc;
        this.writeLogWarn(exc);
        Response.Redirect("~/Pages/Content/TechCorner.aspx");
    }
   
}
thats all for today.
BYE

User ScrumPad for your Agile based projects.

Saturday, April 11, 2009

Consume WCF Service

Hello all

Today I will show how to consume a WCF service. To go through this before, I have created a WCF service [ Creating WCF Service ] , which I will consume for my project in this article. Consuming WCF Service is as simple as consuming a simple webservice through Visual Studio 2008.

First I am taking an empty Web project named ConsumedWCF.

Now I will add a service reference which I have created before [ Creating WCF Service ] in this ConsumedWCF project as bellow:

In my previously created WCF service I have got a URL for my service "http://localhost:49530/DataService.svc?wsdl", I have pasted the URL in the Address box. One thing we should have to remember that,to consume the service we have to run that service during development.

Now I will consume the service from my Default.aspx  . My Default.aspx.cs content is as bellow:

using System;
using ConsumeWCF.ServiceReference1;

namespace ConsumeWCF
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           DataServiceClient cl = new DataServiceClient();
           Response.Write( cl.GetUserDataFromService().UserName);
           Response.Write("<br />");
           Response.Write(cl.GetUserDataFromService().UserLocation);
           Response.Write("<br />");
           Response.Write(  cl.GetUserRestrictionFromService("1"));
           Response.Write("<br />");
           Response.Write(cl.GetUserRestrictionFromService("23"));
           
        }
    }
}

The above code will print the values which we have consumed form the WCF service.

Thanks

That's all for the day.
BYE

User ScrumPad for your Agile based projects.