You must Sign In to post a response.
  • Category: ASP.NET

    Values assign to label via client side are not accesible on server side


    I have one label and assigning some value to it via javascript/jquery.It gets that value and displays me in browser, but when I access that label's text on server side on some button click, it returns blank string.any reason?

    but the same works in case of textBox. but not in case of label.( note: Label renders as Span in browser)
  • #762841
    Hello Anjali,

    Refer the below code :

    <html xmlns="" >
    <head runat="server">
    <script type="text/javascript">
    function setval() {
    var labelval = document.getElementById('<%= TextBoxVal.ClientID %>').value;
    document.getElementById('LabelVal').value = labelval;
    function getval() {
    var labelval = document.getElementById('<%= LabelVal.ClientID %>').innerText;
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBoxVal" runat="server"></asp:TextBox>
    <asp:Button ID="ButtonSetVal" runat="server" Text="Set Value" OnClientClick="setval();"/>
    <br />
    <asp:Label ID="LabelVal" runat="server" ></asp:Label>
    <br />
    <br />
    <asp:Button ID="ButtonGetVal" runat="server" Text="Get Value" OnClientClick="getval();"/>

    Code - behind :

    protected void Page_Load(object sender, EventArgs e)
    if (this.IsPostBack)
    LabelVal.Text = Request.Form[TextBoxVal.UniqueID];

    See the attached image. It's an output i got by using above mentioned code.

    Hope this is what you are looking for.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #762874
    Hey Nirav,

    I agree , you are able to fetch the data in label, but I dont want to use textbox. As I know the fact that textbox works in this case. This approach would not be helpful in my case there are lots of label in my Page, i can't set each ones' value using Textboxes.

    Thanks for your help!

  • #762877
    Hello Anjali,

    I have shown you the way that how you can do it. You can try this code as you want.

    I have shown you the method also that how i am accessing and setting the value to the label.

    You can try it or how you want to access. You can tell other method so we can suggest you something.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762908
    Thanks but I dont want to use textboxes or some other control except labels.....I just want to know why labels are losing their value on server side, but same is retained by textbox ?

  • #762916
    Hello Anjali,

    The label control is a read-only control so we can not access changes by javascript in server side.

    Till i know you've to follow another way for doing this, which i had shown you. Label controls do not send values to server and hence their value is lost on PostBack so you have to make server aware of what happened at client side. You can't overwrite it from client side and maintain its value if a PostBack is executed.

    Hope you understand.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762933
    yes, it actually make sense.

    Thanks :-)

  • Sign In to post your comments