user4221591
user4221591

Reputation: 2150

reusing methods used in one code behind page to another code behind page

In my asp page I've a dropdown whose value is being retrieved from database. To retrieve the value for the dropdown, I have written a method in code behind page.

Now I've to use the same dropdown in another asp page also. For this I am writing the same method to the corresponding code behind page for retrieving the values from the database.

I want to know is there any way so that I can re-use the methods needed in code behind pages?

eg. Product Page(asp page)

<tr>
    <td class="va-top">Type:</td>
    <td><asp:ListBox ID="listBox_ProductType" runat="server" Rows="1" Width="300px"></asp:ListBox></td>               
</tr>

aspx page

public void GetProductBillingType()
{
    try
    {
        DataTable dt = new DataTable();
        listBox_ProductType.ClearSelection();
        DAL_Product_Registration objDAL = new DAL_Product_Registration();
        dt = objDAL.Get_ProductBillingType();
        if (dt != null && dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                listBox_ProductType.Items.Add(new ListItem(row["billing_sub_type"].ToString(), row["billing_dtls_id"].ToString()));
            }
        }
    }
    catch (Exception ex) { }
}

Now in another page, I've to use the same dropdown. I am writing the same method in another code behind page also.

But is there any way I can reuse the methods used in aspx pages.

Upvotes: 3

Views: 1465

Answers (3)

lukai
lukai

Reputation: 576

You can create a static class and keep your helper codes there. So that you don't need to re-invent the wheel. The reason behind to create a static class is that you don't need to create an instance for accessing the class methods. Here's an example.

public static class HelperMethods
{
    public static void GetProductBillingType(ListBox listBox)
    {
        try
        {
            DataTable dt = new DataTable();
            listBox.ClearSelection();
            DAL_Product_Registration objDAL = new DAL_Product_Registration();
            dt = objDAL.Get_ProductBillingType();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    listBox.Items.Add(new ListItem(row["billing_sub_type"].ToString(), row["billing_dtls_id"].ToString()));
                }
            }
        }
        catch (Exception ex) { }
    }
}

Now, you can use this methods in other places just by calling the methods. Pass the ListBox where you want to add the data as parameter.

HelperMethods.GetProductBillingType(list_box_where_you_want_to_add_data);

Upvotes: 2

OzgeTas
OzgeTas

Reputation: 11

The main point of this issue is extracting reusable part of the code into a method in another class like a utility or helper class, and accessing this method from those code behing pages. In addition, you can use a tool like resharper to recommend you how to code better.

Upvotes: 0

ohadinho
ohadinho

Reputation: 7144

Try to extract this functionality to some other method which gets as a parameter the relevant ListBox.

For instance:

public class Helper
{
    public static void GetProductBillingType(ListBox lb)
    {
       ...
    }
}

In your aspx code behind:

public void GetProductBillingType()
    {
       Helper.GetProductBillingType(listBox_ProductType);
    }

And in the other aspx page:

public void GetOtherBillingType()
    {
       Helper.GetProductBillingType(listBox_OtherType);
    }

Upvotes: 2

Related Questions