How to create web form class object and use it.

I have situation.
in my asp.net 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.
ziorinfoAsked:
Who is Participating?
 
gery128Commented:
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.
0
 
ziorinfoAuthor Commented:
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
0
 
gery128Commented:
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.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Anurag ThakurTechnical ManagerCommented:
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
0
 
gery128Commented:
@ziorinfo
ragi0017 has suggested another way of achieving the required.
If you still have confusion, elaborate your problem more.
0
 
Paulmc999Commented:
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
        {
            get
            {
                return _enabel;
            }
            set
            {
                _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;
        try
        {
            testConn.Open();
            updated = cmd.ExecuteNonQuery();
        }
        catch (Exception err)
        {
            //Response.Write(err);
        }
        finally
        {
            //myConnection.Close();
            testConn.Close();
        }
    }
     }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.