[英]Unable to retain value of a label during post back(button click) in sharepoint
While writing code for connecting two webparts,I created a two labels. 在编写用于连接两个Web部件的代码时,我创建了两个标签。 the text value for both the labels were written in OnPreRender method.
两个标签的文本值均以OnPreRender方法编写。 However, i forgot to add control for one label in CreateChildControl method.
但是,我忘了在CreateChildControl方法中为一个标签添加控件。 So while debugging, i noticed that, after post back, the label whose control i forgot to add didn't retain the value and it was showing empty string .But the other label who's control i added was able to retain the value .
因此,在调试时,我注意到在回发之后, 我忘记添加控件的标签并没有保留该值,而是显示了空字符串 。但是我添加了控件的其他标签却能够保留该值 。
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!";
}
}
Why is it behaving like this? 为什么会这样?
This is a normal behavior. 这是正常现象。 If you don't add the control to
Controls
collection, ASP.NET framework will not preserve its value in postback and hence will be lost during postback. 如果不将
Controls
添加到Controls
集合中,则ASP.NET框架将不会在回发中保留其值,因此会在回发期间丢失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.