Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
HI All

I am facing a problem in getting the label value after post back.

I have a label, and i am changing the value in client side ( using jquery , javascript )

when i do post back by button click event, i am not getting the newly assigned value of the label text in code behind ( .cs file )

How to resolve it..

I dont wanna use Hidden field..

code::::::

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="JQuery.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(function () {

            $('#btn').click(function (e) {

                $('#lbl').text('value changed from client side');

                return true;
            });

        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lbl" Text="sometext" runat="server"></asp:Label>
        <br />
        <asp:Button ID="btn" Text="submit" runat="server" onclick="btn_Click" />
    </div>
    </form>
</body>
</html>





C#
protected void btn_Click(object sender, EventArgs e)
        {
            string lblvalue = lbl.Text;
        }



Any ideas ??

Thanks in advance...
Posted
Updated 22-Apr-19 4:17am
v2
Comments
Thanks7872 27-Nov-13 23:03pm    
Where is the code?
Karthik_Mahalingam 27-Nov-13 23:17pm    
code added ......

Its important to know how the Client and Server side code works. Have a look at this link : Set Text property of asp:label in Javascript PROPER way[^]

Exactly the same problem is explained there. I don't know why you don't want to use hiddenfield/Viewstate etc. Take a look at example code i created:
C#
<asp:HiddenField ID="Hidden1" runat="server" />    
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Code behind:
C#
Page.ClientScript.RegisterStartupScript(this.GetType(), "change label", "<script type='text/javascript'>change_label()</script>");
Label1.Text = Hidden1.Value;//This is how you will get the updated value 

JavaScript:
C#
<script type="text/javascript">
function change_label() {      
    document.getElementById("Label1").innerText = "This is updated value";           
}   
</script>
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 27-Nov-13 23:33pm    
I dont want to use hidden field, because i have more than 10 labels to be updated in client side..
Thanks7872 27-Nov-13 23:38pm    
This is how ASP.NET works. Server and client are totally different things. In very simple terms,you have to make server aware of what happened at client side. I updated the solution in order to show you the example code.
Karthik_Mahalingam 27-Nov-13 23:47pm    
for label it wont work i guess...
any how i wil go with the hidden field,
thanks for spending your time to give a possible solution.
Thanks7872 27-Nov-13 23:49pm    
Copy paste the above code.Its working. Try to understand what i have done.
Joezer BH 22-Jan-14 10:50am    
5ed!
I just did this exact thing. Here's what I did to pull the value I'm setting to my label...

For each label you are updating, you need to also create a hidden field. A simple html hidden field will suffice.
<asp:Label runat="server" ID="cust1" Text="" AutoPostBack="false"></asp:Label>
<input type="hidden" name="hidCust1" id="hidCust1" runat="server" enableviewstate="true" />


Ensure that "enableviewstate" is set to "true" for the hidden field.

In your javascript, after you set the value of the label:
if (cust1.innerHTML == '') {
    cust1.innerHTML = desc;
    icust1.style.visibility = 'visible';
}


also set the value of the hidden field:
document.getElementById("hidCust1").value = cust1.innerText


Then, in your code behind, simple request the value of the hidden field:
sRetVal = hidCust1.Value
 
Share this answer
 
You can use a viewstate to retain the value of the label as explained here
 
Share this answer
 
Comments
Karthik_Mahalingam 27-Nov-13 21:38pm    
No dude, its not working. Do u have sample??

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