如何在Asp.net中创建一个类并在每个页面中调用它. [英] How to Create a Class in Asp.net and Call in it in Every Page.

查看:40
本文介绍了如何在Asp.net中创建一个类并在每个页面中调用它.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Asp.net中创建类并在每个页面中调用它.
================================================== ===========

亲爱的Frnds,我正在研究Asp.net,C#和SqlServer2005.

我已经创建了一个Web应用程序,现在我必须在每个页面中调用以下代码.所以我需要使用C#在asp.net中创建一个类.并在每个页面中调用它.

我这个Iam使用数据库时间并显示在网页上.

这是我的代码.

How to Create a Class in Asp.net and Call in it in Every Page.
=============================================================

Dear Frnds, Am working on Asp.net , C# , SqlServer 2005.

I Have Created a Webapplication, Now I have to Call this below code in every page. So I need to create a class in asp.net using C#. and call it in every page.

I this Iam using Database time and displaying on webpage.

This is my code.

using Sytem.Data.SqlClient;
using System.web.configuration;
using System.Drawing;

protected void Page_Load(object sender, EventArgs e)
    {
       
SqlConnection con = new SqlConnection("Data Source=IT-pc;Initial Catalog=MaintaienanceDB;User ID=sa; Password=12345;");

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "Select Datalupdate from DataLastUpdate";

        using (con)
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            if (reader.Read())
            {
                lblUpdate.Text = reader["Datalupdate"].ToString();

                lblUpdate.ForeColor = Color.Red;
            }
        }
       
    }


请帮助,如何创建一个类并在所有网页中调用它.

谢谢.


Please help, How to create a class and call this in all webpages.

Thanks.

推荐答案

一个更好的主意是只需要在每个页面中重用此代码,便可以创建一个包含该代码的PageBase类,并且只需从该PageBase类派生所有其他页面.
A better idea if you are talking about just needing this code to be reused in each page would be to create a PageBase class that includes this code and simply derive all your other pages from that PageBase class.
public partial class PageBase : System.Web.UI.Page
{
   public PageBase()
   {
      this.Load += new EventHandler(this.Page_Load);
   }

   protected void Page_Load(object sender, EventArgs e)
   {
      // Your common stuff here
   }
}

public partial class YourPage : PageBase
{
   protected void Page_Load(object sender, EventArgs e)
   {
      // Your page specific stuff here.
   }
}


您可以在网上使用sqlhelper class.search轻松获得它,然后可以根据需要在sqlhelper类中调用方法
You can use sqlhelper class.search on net you easily got it and then you can call methods in the sqlhelper class as per your requirement


1.创建一个自定义属性并在其中写下您的方法.
2.创建一个基类并在其上应用上面的属性.
3.从该基类派生所有页面.
4.您应该已经完成​​.
1. Create a custom attribute and write down your method there.
2. Create a base class and apply above attribute on it.
3. Derive all your pages from this base class.
4. and you should be done.


这篇关于如何在Asp.net中创建一个类并在每个页面中调用它.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆