I am using a MasterPage site. in default.aspx code-behind, Page_Load event, i create many container DIV (ie. each DIV is a SQL result):
HtmlGenericControl div = new HtmlGenericControl("div");
div.Attributes.Add("id", "div_" + postid);
Then create more DIV in code behind (divBottom) programatically and add it to the container DIV, with the following controls inside it:
LinkButton lnkConfirmo = new LinkButton();
lnkConfirmo.ID = "lnkConfirmo_" + postid;
lnkConfirmo.Text = "Foo";
lnkConfirmo.Click += (sender, e) => { UpdateLabel(sender, e, postid); };
Label lblConfirms = new Label();
lblConfirms.ID = "lblConfirms_" + postid;
lblConfirms.Text = "<small><a href=\"javascript:;\">" + GetConfirmTotal(postid) + " pessoas</a> confirmam.</small>";
lblConfirms.Attributes.Add("runat", "server");
And finally add the bottom div to the container.
div.Controls.Add(divBottom);
On the UpdateLabel() method, i am not being able to change Label text when LinkButton is clicked.
public void UpdateLabel(object sender, EventArgs e, int postid)
{
object ctl = (this.FindControl("ContentPlaceHolder1_lblConfirms_" + postid.ToString()) as Label);
HtmlGenericControl ctl2 = (HtmlGenericControl)this.FindControl("ctl00").FindControl("ContentPlaceHolder1").FindControl("lblConfirms_" + postid);
//I tried more combinations of this.
}
I tried Label and HtmlGenericControl because Label is rendering as a span.
This is the Html output:
<div id="divConfirms_24"><span id="ContentPlaceHolder1_lblConfirms_24" runat="server"><small><a href="javascript:;">190 users</a> confirmam.</small></span></div>
Some controls are available on this.Page.Form (such as some divs, ContentPlaceHolder, ScriptManager and many "Literal), but not the Label as it isn't being rendered as a Control, i guess.
How can i access that span to change its text, or make this an available control to be accessed?
UPDATE: The DIV is added to an UpdatePanel, that is added to a larger container.
UpdatePanel upldivPost = new UpdatePanel();
upldivPost.ContentTemplateContainer.Controls.Add(div);
divPosts.Controls.Add(div);
When it's not in a UpdatePanel, the Label can be accessed (and is changed after postback) by:
Label found = (Label)this.FindControl("ctl00").FindControl("ContentPlaceHolder1").FindControl("lblConfirms_" + postid);