2014-09-25 71 views
0

我有用戶控件ctrlinserimento與一個文本框,我有另一個用戶控件ctrlricerca與應該在ctrlinserimento中設置textbox.text到一個字符串(比如說'你好')。從另一個用戶控件設置一個用戶控件的文本框

我該怎麼辦?

ctrlinserimento

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebApplication3 
{ 
    public partial class ctrlinserimento : System.Web.UI.UserControl 
    { 

    public string textbox1 
    { 

     set { TextBox1.Text = value; } 
     get { return TextBox1.Text; } 
    } 


    } 
} 

ctrlricerca

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using DevExpress.Web.ASPxGridView; 



namespace WebApplication3 
{ 
public partial class ctrlricerca: System.Web.UI.UserControl 
{ 
    public ctrlinserimento i 
    { 
     set; get; 
    } 


    protected void Button1_Click(object sender, EventArgs e) 
    { 
     i.textbox1 = "Hello"; 

     } 
    } 

} 

} 

我的NullReferenceException上

i.textbox1 = "Hello"; 
+0

可以anyboy幫助嗎? – Carmi 2014-09-25 16:34:50

回答

0

有很多的方式來做到這一點,即網頁級別,用戶控制級別。檢查適合您的以下解決方案。

在ctrlricerca用戶控件添加屬性ctrlinserimento和頁面級別分配ctrlinserimento的實例ctrlricerca財產

公共部分類ctrlricerca:System.Web.UI.UserControl {

private string nome; 

public ctrlinserimento { set; } 

protected void Page_Load(object sender, EventArgs e) 
{ 

} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    ctrlinserimento.textbox.text="Hello"; 

    } 


    } 

} 

}

+0

謝謝,但這不能正常工作 – Carmi 2014-09-26 11:12:16

+0

你正在得到什麼問題?我可以幫你 – Mudeyanse 2014-09-28 12:53:22

+0

謝謝你,但我現在用了一種不同的方法...... – Carmi 2014-09-29 16:33:02