您的位置:程序门 -> .net技术 -> c#



c#被委托调用的方法如何知道是哪一个类调用了它?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


c#被委托调用的方法如何知道是哪一个类调用了它?[已结贴,结贴人:leewonburg]
发表于:2007-08-29 14:56:54 楼主
这个问题是我在写一个gridview分页器的时候遇到的,这个分页器包括十几个linkbutton,我的思路是,linkbutton.onclick= "show_content(自己写的分页代码方法) ",show_content中是根据linkbutton的text属性,读取数据库中相应的记录,跳转到相应的页。如:
protected   show_content(object   sender,eventargs   e)
{
int   ipage=convert.toint32(linkbutton.text);
/*
关键是上面这句,怎么访问相应的linkbutton?用this.text应该是不行的
吧,因为这个show_content函数是在另一个类里定义的,而不是linkbutton类的方法。
试过了object.tostring(),可是返回的是类名而不是对象名。
*/
get_data(ipage);
//绑定,填充。。。。
}

这个问题怎么解决?
发表于:2007-08-29 15:03:561楼 得分:0
你可以把你的参数放到eventargs   里面
发表于:2007-08-29 15:10:002楼 得分:20
在form1.designer.cs中有下面两段代码
                        this.button1.location   =   new   system.drawing.point(75,   59);
                        this.button1.name   =   "button1 ";
                        this.button1.size   =   new   system.drawing.size(75,   23);
                        this.button1.tabindex   =   0;
                        this.button1.text   =   "button1 ";
                        this.button1.usevisualstylebackcolor   =   true;
                        this.button1.click   +=   new   system.eventhandler(this.button1_click_1);
                        //  
                       
                        //   button2
                        //  
                        this.button2.location   =   new   system.drawing.point(75,   118);
                        this.button2.name   =   "button2 ";
                        this.button2.size   =   new   system.drawing.size(75,   23);
                        this.button2.tabindex   =   2;
                        this.button2.text   =   "button2 ";
                        this.button2.usevisualstylebackcolor   =   true;
                        this.button2.click   +=   new   system.eventhandler(this.button2_click);
你只要把其中的
  this.button1.click   +=   new   system.eventhandler(this.button1_click_1);
this.button2.click   +=   new   system.eventhandler(this.button2_click);
改成
this.button1.click   +=   new   system.eventhandler(this.button1_click_1);
this.button2.click   +=   new   system.eventhandler(this.button1_click);
然后在
  private   void   button1_click_1(object   sender,   eventargs   e)
                {
                        if(((button)sender).text== "button1 ")
                                //
                        if(((button)sender).text== "button2 ")
                              //

                }
就可以了
原理就是将所有按钮的委托中执行的函数都指定为同一个,然后在这个函数中判断是点击了哪个按钮,执行相应的操作
发表于:2007-08-29 15:18:263楼 得分:0
好的,我试试。
发表于:2007-08-29 17:10:204楼 得分:0
问题解决了!谢谢!
哎,我怎么就没有想到呢。


快速检索

最新资讯
热门点击