Calling a method in parent page from user control

前端 未结 10 1177
暖寄归人
暖寄归人 2020-11-28 23:01

I\'ve a user control registered in an aspx page On click event of a button in the user control, how do i call a method which is there in the parent page\'s code

10条回答
  •  一向
    一向 (楼主)
    2020-11-28 23:35

     //c#
     //In parent page
     public void test(string S)
     {
        Label1.Text = S;
      }
    
     //In user control
     protected void Button1_Click(object sender, System.EventArgs e)
     {
     //Calling "test method" of parent page  from user control  
     ((object)this.Page).test("Hello!!");
     }
    
     'VB.Net 
    'In parent page
     Sub test(ByVal S As String)
        Label1.Text = S
     End Sub
    
     'In user control
      Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
      'Calling "test method" of parent page  from user control  
      DirectCast(Me.Page, Object).test("Hello!!")
      End Sub 
    

提交回复
热议问题