要在formview中将控件的属性在页面初始加载的时候设置,如:要将formview的EidtTemplate中Label1的Text设置为111,
aspx
        <asp:FormView ID="FormView1" Runat="server">
            <EditItemTemplate>
                <asp:Label ID="Label1" Runat="server" Text="Label"></asp:Label>
            </EditItemTemplate>
......
cs:
    private void Page_Load(object sender, EventArgs e)
    {
        Label l1 = (Label)FormView1.FindControl("Label1");
        if (l1 != null)
        {
            l1.Text = "111";
        }
        else
        {
            Response.Write("找不到控件");
        }
    }
l1总是为null,在csdn上搜索只有关于datagrid的,msdn上结合formview讲的只有找pagertemplate上的控件的,没有itemtemplate/inserttemplate/edittemplate找控件的, 在google上搜索也看到老外有这个问题没解决,忘高手们帮忙啊,在线等,解决马上结贴,顶者有分

解决方案 »

  1.   

    先感谢楼上几位的帮忙,忘了说明:是在asp.net2.0里面,webdevelopment里就有这个控件to  SnApnet(什么都不知道) 我查了object browser里面有findcontrol的不多,
    System.Web.UI.Control.FindControl(string)
    System.Web.UI.Control.FindControl(string, int)
    System.Web.UI.Page.FindControl(string)
    System.Web.UI.WebControls.CheckBoxList.FindControl(string, int)
    System.Web.UI.WebControls.RadioButtonList.FindControl(string, int)我这里用到的是System.Web.UI.Control.FindControl(string),
    public virtual System.Web.UI.Control FindControl(string id)
        Member of System.Web.UI.ControlSummary:
    Searches the current naming container for a server control with the specified id parameter.Parameters:
    id: The identifier for the control to be found. Return Values:
    The specified control, or null if the specified control does not exist.
    我也觉得是层次的问题,但是不知道naming container是什么。
    所以找不到
      

  2.   

    哦,原来如此,我想这个东西跟datagrid/datalist等也是一样的.
    你在page_load里面去找它,应该:
    for(int i=0;i<this.FormView1.Items.Count;i++)
    {
    Label Label1 = (Label)this.FormView1.Items[i].FindContr("Label1");
    Label1.Text = "111";
    }或者这样:
    Label Label1 = (Label)this.FormView1.Items[0].FindContr("Label1");但前提是,你这个FormView1必须事先有数据,否则它是空的,根本没有任何控件.
      

  3.   

    不一定要有数据吧,我把DefaultMode 设置为Insert就可以了吧
      

  4.   

    formview类似于datelist的其中一项,可以自己定格式的,可以是自己画的不规则的表,如果多项,可以用翻页来实现,就是说每页只能显示一条记录,可以插入,修改,删除(分为不同的template来设置,跟数据源绑定后可以不用写很多代码),现在其实我是想在插入一条记录时,部分信息是从另一个表读出来的,直接显示上去, 不用填(这里要在后台作些参数设置)。关键问题就是要找到控件
      

  5.   

    没用过2。0 不过 在DG和DL里都会指定要在哪个单元格里,你下面的这句没有
    Label l1 = (Label)FormView1.FindControl("Label1");
    我也不知道FORMVIEW是怎么样的控件,也许不需要提供这个属性,但是如果有的话我想应该要加上
      

  6.   

    如果确实不能得到,你可以用如下方法试验.
    这些服务器控件,都会以子控件,孙控件,孙孙控件这样的形式出现,所以,你可以一层一层的自行判断,看看到底在哪个控件里面,才隐藏我们放进去的控件.
    this.response.write(this.FormView1.Controls.Count.ToString());
    不断地一层一层探测
    还有
    this.FormView1.Controls[0].GetType().ToString();
    this.FormView1.Controls[0].Controls[0].GetType().ToString();
    等,相信这样做,用不了半小时,就可以全部知道了.
      

  7.   

    非常感谢各位的帮助,我已经解决了,在msdn上看出问题来的,先结贴,然后写出解决方案来
      

  8.   

    问题1.在Page_Load的时候formview里的数据行对象还没有实例化,所以不管在Page_Load里写的如何,都不会成功的,改成在Formview 的DataBound时就可以了
    问题2.控件是在数据行里的,即Formview.Row.Findcontrol("controlid")就可以了,代码如下
        void FormView1_DataBound(object sender, EventArgs e)
        {
            FormViewRow row = FormView1.Row;
            Label l1 = (Label)row.FindControl("Label3");
            if (l1 != null)
            {
                l1.Text = "1111";
            }
            else
            {
                Response.Write("找不到控件");
            }
        }
    我还没用过 hchxxzx(NET?摸到一点门槛) 的方法看过控件,现在试试
    总之,非常感谢各位,以前问的几个问题都是asp.net2.0里的,很少有人光顾,今天这么多人来帮我, 很高兴!!!