1

I want to update my label content when the user submits a form but it doesn't get updated. Although I have put it in if (!IsPostBack) condition in form load It doesn't show the changes. The only solution that I came up with was defining a counter and increase it in button_click event and check it before label update in !IsPostBack condition. Which is working fine with that. Is there any other way to update the label text?

Here is my solution:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        {
           if (count > 0)
           lblSuccessMsg.Text ="A Message!";
           count = 0;
         }
 }

protected void btnSubmit_Click(object sender, EventArgs e)
{
    Save();
    count = count + 1;
}

2 Answers2

0

Hard to tell but by the sounds of it you have a submit button and the onclick needs to update this label, something like this should work. I'm using viewstate but session would work here, as would a redirect to the same page with a querystring parameter. Not sure if I've understood your question correctly though.

protected void Page_Load(object sender, EventArgs e)
{
  if(!IsPostBack)
  {
    if(Viewstate["updateLabel"] == "true")
    {
      lblYourLabel.Text = "I'm updated now!";
      Viewstate["updateLabel"] = "";
    }
  }
}

protected void btnYourButton_Click(Object sender, Eventargs e)
{
  ViewState["updateLabel"] = "true";

  //Do other stuff here if you want
}
Full Time Skeleton
  • 1,680
  • 1
  • 22
  • 39
0

please update your label code outside of !IsPostBack evnt and inside a pageload.

Manish Sharma
  • 2,406
  • 2
  • 16
  • 31
  • this is not the case. It should be in the !IsPostBack otherwise it will be called each time the page_load event is triggered which is not my purpose. –  Jul 05 '13 at 10:22