ASP.NET - how to avoid label value clear when page refresh

Asked By msakt on 16-Apr-12 01:29 AM
how to avoid label value clear when page refresh
Danasegarane Arunachalam replied to msakt on 16-Apr-12 01:34 AM
If the value is getting cleared on postback means that you are not binding the label in If  not PostBack event

Example

I Not Page.IsPostBack Then
    ' The Page is loading for the first time.
    'Bind the Label Here
   Label1.Text="my Label"
 
End If
Reena Jain replied to msakt on 16-Apr-12 01:34 AM
hi,

By using Update panel you can prevent the part of the page from the PostBack so the portion of the webpage define inside the update panel won't get refreshed by the postback. so you can put your label in update panel to prevent

but at the time of reload the page the entire webpage (including update panel) will be refreshed
Somesh Yadav replied to msakt on 16-Apr-12 02:25 AM
hi
For avoiding the page refresh, place all your controls inside update panel like the below

<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>

<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>

<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>

</ContentTemplate>
</asp:UpdatePanel>
Sri K replied to msakt on 16-Apr-12 02:36 AM
You need to set the value in the Page IsPostback property value.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   If Not IsPostBack Then
     lblWelcome.Text = "Welcome to World!"
   End If
 End Sub
Anoop S replied to msakt on 16-Apr-12 02:41 AM
Do you have label databind in Page_Load? If yes, put the logic in

if (!IsPostBack)
{

}
dipa ahuja replied to msakt on 16-Apr-12 03:52 AM
protected void Page_Load(object sender, EventArgs e)
{
  if (IsPostBack)   
   {   
    if (!String.IsNullOrEmpty(lable1.Text))   
    {
     lable1.Attributes.Add("value", lable1.Text);   
    }   
   }
}