Issue: When an asp.net page does a postback, only input fields (e.g. textboxes) will return their current value (as updated by the user or javascript etc). If you have a label (not an input field) (in my case it was a word count) that you want to be able to access after a postback on the server you'll need a workaround.
Solution: It's a bit ugly but use a TextBox and set its style to "display:none" and then update this at the same time you update your label. Don't set it to Visible="false" because then the whole control won't render at all. You can't use a HiddenField because it isn't an input field and hence on postback its updated value won't be returned. Then in Page_Load set your label again based on the value of this hidden TextBox (remember it will need to be done when Page.IsPostBack == true).
ASPX:
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
//Because we update the word count via javascript, it won't persist between postbacks (which occur when
//we change the club selected etc). Therefore we store it in a hidden text field and update the label
//each time the page is reloaded.
lblWordCount.Text = txtCurrentWordCount.Text;
}
}
No comments:
Post a Comment