How to create web form class object and use it.

Posted on 2011-10-20
Last Modified: 2012-05-12
I have situation.
in my application I have web page which has a master page.
User can create an PDF file at the run time by using this form. on the form i have few grid, labels controls which I am using to get data for PDF file.

Now my clients said he want to create the same pdf file from a different web form.
I am trying to avoid copying same code.

I need to know can create an object of my web form class in my second web form and call the Create pdf method to create the pdf file.

but I also want to load all controls so that pdf have the correct values.
Question by:ziorinfo
    LVL 9

    Accepted Solution

    Why do you want to create object and populate all those controls in other page?

    You can utilize the same webform for creating the pdf in new page. What you can do is, on second page, on clicking some link like create pdf or on page load, display first form in modal dialog or if the second form has nothing (its blank) than on page load of second form just make Server.Transfer("/FirstForm.aspx"); URL would be the same in browser.

    Author Comment

    the issue with me here is create pdf method needs to be called after executing few line of code which does some database update.

    and first form which has create pdf form has controls and I need to load these controls so that while creating pdf I can pick values from them
    LVL 9

    Expert Comment

    So, you have some controls on second form ( non-pdf form) ? And want to use it in pdf-creating-form?
    If you want to execute DB updates, you can do it in page-life-cycle event early in second form and then load the first form (pdf-creating-form) in a modal which will show up to user with all contents. You don't need worry about first form's (pdf-creating-form) controls to be populated and other things, that would be done automatically. just chose the a good modal dialog from several JS based techniques.
    LVL 26

    Expert Comment

    by:Anurag Thakur
    What I have understood is that your PDF generation logic and the look and feel of the page generating the PDF (data capturing etc) is the same on both the pages

    If this is correct, my suggestion will be convert the existing code into a user control and the reuse the functionality in the new page
    LVL 9

    Expert Comment

    ragi0017 has suggested another way of achieving the required.
    If you still have confusion, elaborate your problem more.
    LVL 2

    Expert Comment

    So I am not sure what you are looking for, advise as to whether to create a class or how to create a class.

    To me if I understand what you have done so far it seems a general class that you can call from any page would seem to be a good idea.

    To create a class you go to add new item and select class. See part example of a class I use called ddItems and I have named the namespace ListClasses;

    In the page I wish to create the instance of the class I add

    using ListClasses;

    then I can create my instance like this:

    doc.Ditem = new List<ddItems>();

    Does any of this help? Obviously the class you define will be very different than these but it shows you the type of thing you need to do to get started.

    using System.Data.SqlClient;
    using System.Configuration;
    namespace ListClasses
        public class ddItems
            private int _rowno;
            private int _dateno;
            private int _ddindex;
            private string _absence;
            public ddItems()
                Rowno = 0;
                Dateno = 0;
                Ddindex = 0;
                Absence = "Not available";
            public int Rowno        {
                get { return _rowno; }
                set { _rowno = value; }
            public int Dateno
                get { return _dateno; }
                set { _dateno = value; }
            public int Ddindex
                get { return _ddindex; }
                set { _ddindex = value; }
            public string Absence
                get { return _absence; }
                set { _absence = value; }
        public class Doctor
            private int _persno;
            private string _name;
            private IList<ddItems> _ditem;
            private Boolean _enabel;
            public Doctor()
                Persno = 0;
                Name = "Not available";
                Ditem = null;
            public int Persno
                get { return _persno; }
                set { _persno = value; }
            public string Name
                get { return _name; }
                set { _name = value; }
            public IList<ddItems> Ditem
                get { return _ditem; }
                set { _ditem = value; }
            public Boolean Enabel
                    return _enabel;
                    _enabel = value;
            public void LoadDoctors()
            string insertSQL = "UPDATE person SET LastnameT = 'Test' WHERE LastnameT = 'Browne'";
            SqlConnection testConn = new SqlConnection();
            testConn.ConnectionString = ConfigurationManager.ConnectionStrings["DocConnection"].ConnectionString;
            SqlCommand cmd = new SqlCommand(insertSQL, testConn);
            int updated;
                updated = cmd.ExecuteNonQuery();
            catch (Exception err)

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Session variables expiration ASP.NET C# 3 37
    c# string 6 26
    What is MicroStrategy.NET? 2 20
    send mail from 8 0
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now