Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sharepoint中回发(按钮单击)期间无法保留标签的值_Sharepoint_Postback_Web Parts_Createchildcontrols - Fatal编程技术网

在sharepoint中回发(按钮单击)期间无法保留标签的值

在sharepoint中回发(按钮单击)期间无法保留标签的值,sharepoint,postback,web-parts,createchildcontrols,Sharepoint,Postback,Web Parts,Createchildcontrols,在编写连接两个Web部件的代码时,我创建了两个标签。两个标签的文本值都是用OnPreRender方法编写的。但是,我忘记在CreateChildControl方法中为一个标签添加控件。 因此,在调试过程中,我注意到,回发后,我忘记添加控件的标签没有保留值,它显示空字符串。但我添加控件的另一个标签能够保留值 protected override void CreateChildControls() { base.CreateChildControls();

在编写连接两个Web部件的代码时,我创建了两个标签。两个标签的文本值都是用OnPreRender方法编写的。但是,我忘记在CreateChildControl方法中为一个标签添加控件。 因此,在调试过程中,我注意到,回发后,我忘记添加控件的标签没有保留值,它显示空字符串。但我添加控件的另一个标签能够保留值

protected override void CreateChildControls()
    {
        base.CreateChildControls();




        btnup.Text = "  Update";

        this.Controls.Add(lblid);//**If i add this, the label retains the value during post back , otherwise its null**


        this.Controls.Add(lblname);
        this.Controls.Add(lbldesig);
        this.Controls.Add(tbdes);
        this.Controls.Add(lblcomp);
        this.Controls.Add(tbcomp);
        this.Controls.Add(btnup);

        btnup.Click += new EventHandler(btnup_Click);
    }


protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (connectionInterface != null)
        {

            id = connectionInterface.parameter1;

            SPWeb mysite = SPContext.Current.Web;
            SPList mylist = mysite.Lists["UpdateList"];
            SPListItemCollection itemcol = mylist.Items;

            foreach (SPListItem itm in itemcol)
            {
                string nm = itm["Company_Id"].ToString();
                if (nm.Equals(id))
                {
                    lblid.Text = itm["Company_Id"].ToString();
                    lblname.Text = itm["Name"].ToString();
                    l
                }

            }


        }
        else
        {
            lblname.Text = "nothing is recieved!";
        }

    }

为什么会这样?

这是正常的行为。如果不将控件添加到
控件
集合,ASP.NET framework将不会在回发中保留其值,因此在回发过程中会丢失