2011-01-28 98 views
0

我創建了一個用戶控件。它包含一個文本框。我可以從父網頁控制這個嗎?用戶控制在Asp.net

+0

什麼問題..? – awe 2011-01-28 08:33:10

回答

3

以下屬性添加到您的用戶控件:

public string SomeValue 
{ 
    get 
    { 
     return txtSample.Text; 
    } 
    set 
    { 
     txtSample.Text = value; 
    } 
} 

如果您想要從包含用戶控件的頁面獲取或設置用戶控件的文本框值,只需執行以下操作:

MyUserControl.SomeValue = "Hello from page"; 
lblTest.Text = MyUserControl.SomeValue; 

其中「MyUserControl」是包含頁面中用戶控件的ID。

注意:由於TextBox控件在處理自身的ViewState的Text屬性,你不必明確地處理它的這種性質。

1

只要定義用戶控件的一些公共方法或屬性,你可以成功地訪問它。例如:

在UserControl1.ascx.cs:

public void DoSomething() 
{ 
    //Do something here from UserControl 
} 

在父:

MyInstanceOfUserControl1.DoSomething(); 
+0

號在後回來,我想從 – zanhtet 2011-01-28 08:40:04