Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All

I am setting some text to server side label in javascript but on postback i am losing its values
Posted
Updated 18-Sep-11 23:41pm
v2
Comments
Prerak Patel 19-Sep-11 5:41am    
Removed pre tags.
rkthiyagarajan 19-Sep-11 5:43am    
Show your code...
mayur csharp G 19-Sep-11 5:50am    
i am setting text to label in this way in javascript
document.all.txtfrshldnm.innerText = splitval[0];
document.all.txtscdhldnm.innerText = splitval[1];
document.all.txtthrdhldnm.innerText = splitval[2];


on button click it is getting empty
rkthiyagarajan 19-Sep-11 5:55am    
You may try this in code behind... Not possible to access javascript in server side.

Hi,

We can not access changes by javascript in server side.

upto my knowledge you've to follow another way for doing this.like posting changed data to server by jquery.

here 'm providing some example to maintain javascript values on postback using jquery .

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script language="javascript" >
        function setval() {
            $("#Label1").text("hi this is testing");
            $.post("default3.aspx?action=change", {txt:"hi this is testing"}, function (data) { });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type ="button" value ="set Value" id="bthsetval" onclick="setval()" />
        <asp:Label ID="Label1" runat="server" Text="Label"><br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>


And we've to maintain one session variable for label text value

we've to write some code in code behind file to set session variable to label text

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["action"] != null)
    {
        Session["lbltext"] = Request.Form["txt"].ToString();
    }

}
protected void Button1_Click(object sender, EventArgs e)
{
    if (Session["lbltext"].ToString() != "")
    {
        Label1.Text = Session["lbltext"].ToString();
    }
}


By this way we can maintain javascript changes in server side.
All the best
 
Share this answer
 
v2
If your text is fixed make the label property read only to true and try

if u are ajax in ur page then try:protected void Page_Load(object sender, EventArgs e)
{
if (Session["lbl"] != null)
{
label1.Text = Session["lbl"].ToString();
Session.Remove("lbl");
}
}


and in the event where postback occurs : Session.Add("lbl", label1.Text);
 
Share this answer
 
v2
when your page get's postback(when page load event gets called), collect existing value from lable and reassign it again.

C#
onPage_Load()
{
  lblVal.Text = request.Form["lblVal"]
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900