如何在不同的类中访问gridview [英] how to access gridview in different class

查看:83
本文介绍了如何在不同的类中访问gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共部分类WebFormAdmin_frmInternalMemo:System.Web.UI.Page { C }



在此class,我有一个ID = gvListMemo 的gridview



我需要在名为 public的类中访问此gridview class Task {} 放在文件夹AppCode / Utility / Task.cs



如何访问它????

解决方案

你可以尝试这样..



  public   class 任务
{

public void DoSomeAction(System.Web.UI.WebControls.GridView gridView)
{
// 使用gridView对象,您可以执行所需的操作

}

}




网格页面中的






任务objTask =  new  Task(); 
objTask.DoSomeAction(gvListMemo);


你好,



正如Karthik先生所说,你可以通过那个控制对于另一个类中的方法。就像这样:



 public class c1()
{
public void method(控制c)
{
////对您的控制执行操作
}

}

主要:

c1 c = new c1();
c.method(any_control); ///传递你的控制权

我也可以这样做:


公共类c1()
{

控制c;
public c1(对照c)
{
this.c = c;

}


public void method()
{
////使用this.c做你想做的任何事,但是你必须将它输入到你想要的控件中。例如对一个文本框说,所以

TextBox t =(TextBox)this.c; ///试试这个,有时会在写这个句子时引发运行时异常警惕。您必须了解基类和派生类之间的关系。

}

}


在Main方法中:
c1 c = new c1(any_control);





请注意,Control类是所有控件的基类(如果我错了,请更正我)。

所以为了使这个算法通用,我可以使用Control类型成为构造函数或方法的参数。



- 我使用了构造函数,因为它可能会发生在课堂初始化期间你想用那个控制执行一些操作





谢谢,

- 拉胡


public partial class WebFormAdmin_frmInternalMemo : System.Web.UI.Page { C}

In this class, I've a gridview with ID="gvListMemo"

I need to access this gridview in class named "public class Task { }" which is placed in the folder AppCode/Utility/Task.cs

how do I access it ????

解决方案

You can try like this..

public class Task
   {

       public void DoSomeAction(System.Web.UI.WebControls.GridView gridView)
       {
            // using the gridView object you can perform your desired action

       }

   }



in the Grid Page,


Task objTask = new Task();
           objTask.DoSomeAction(gvListMemo);


Hello,

As Karthik sir said, you can pass that control to the method in another class.Like this :

public Class c1()
  {
   public void method(control c)
   {
       ////perform action on your control
   }
 
  }

  Main:
  
  c1 c = new c1();
  c.method(any_control); ///pass your control

 I can also do something like this : 


  public class c1()
   {
 
   control c;
      public c1(control c)
         {
         this.c = c;

         }


       public void method()
          { 
            ////use this.c to do anything you want, but you will have to type case it to the control you want. for example say to a textbox so
         
            TextBox t = (TextBox)this.c; /// try this out, sometimes throws runtime exception be vigilant while writing this sentence. You must understand the relation between base and derived classes.
            
          }

    }

  
  In the Main method :
  c1 c = new c1(any_control); 



Please note, the Control class is the base class of all controls(correct me if i am wrong).
So to make this algorithm generalized i can use the Control type to become the argument of a constructor or a method.

- I have used a constructor because it may happen that during class initialization you want to perform some operation with that control


Thanks,
-Rahul


这篇关于如何在不同的类中访问gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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