我在DataList下的<SelectedItemTemplate>模板下有个TextBox1,我想给它赋值,但是直接在外面编时提示没用TextBox1,在网上看到一点
好像说用FindControl来得子控件ID,但我用下面
this.DataList1.SelectedIndex = e.Item.ItemIndex;
((TextBox)e.Item.FindControl("TextBox3")).Text = "hello";
未将对象引用设置到对象的实例。 如何解决,高手说下!顺便说下:我原意是在点项模板下的一个按钮,然后调用事件
DataList1_ItemCommand(object source, DataListCommandEventArgs e)
显示在TextBox1中.我没什么分,有不高手帮下??

解决方案 »

  1.   

    你试下把TextBox1的里面有个属性设置为piblic比如是Form1里面的TextBox1  就直接写Form1.TextBox1 就可以了。 
      

  2.   

    你说的是TextBox1中的哪个属性,试了下还是不行
    System.Web.UI.HtmlControls.HtmlForm”并不包含“TextBox2”的定义
      

  3.   

    不行的话这样试下
    label lab1 = (label)his.DataList1.rows[e.Item.ItemIndex].FindControl("TextBox3");
    lab1.Text = "hello"; 
      

  4.   


    我是想给DataList1中的子控件TextBox1赋值,不是在外赋值.你说的SelectedItem.FindControl,我用下面的语句还是提示:  未将对象引用设置到对象的实例。
    ((TextBox) DataList1.SelectedItem.FindControl("TextBox2")).Text=getresult ;
      

  5.   

     
     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    TextBox txt= (TextBox )e.Item.FindControl("TextBox3");
    txt.Text= "";
    }
      

  6.   

    protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                TextBox tb = e.Item.FindControl("TextBox1") as TextBox;
                tb.Text = "csdn";
            }
        }
      

  7.   

    上面2个可以运行,我也试了下,但好像不是给DataList1中的TextBox3赋值,而是给txt.Text赋值了.
    ((TextBox) DataList1.SelectedItem.FindControl("TextBox2")).Text=getresult ; 
    这样就不行,不知为什么