asp.net - How to change property of UserControl on PostBack? -
let's have page custom usercontrol
(containing asp:literal
, string property) , button. want change literal's text on button click, change control's string property in button clicked event. this:
//in aspx protected void button1_click(object sender, eventargs e) { testcontrol.text = "triggered"; }
the problem literal remains unchanged because page_load
event fires first , creates custom control, button_clicked
fires , changes property control created, nothing. control's code behind:
public partial class testcontrol : system.web.ui.usercontrol { public string text { get; set; } } protected void page_load(object sender, eventargs e) { lbltest.text = text; } }
i figured out if move logic in custom control page_load
property setter change intended. this:
public string text { { return lbltest.text; } set { lbltest.text = value; } }
is there other (better) way this? real problem involves more complicated controls described here, problems remains same: on postback properties set in event handlers ignored.
moving logic page_load
page_prerender
solved problem. properties set before logic executes. i'll wait other answers check if there better solution , if there drawbacks of using page_prerender
.
Comments
Post a Comment