[RESOLVED]empty values after postback

in this code = >

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication6.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="Scripts/jquery-2.0.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%= TextBox1.ClientID%>").blur(function () {
                $("#<%=Label1.ClientID%>").text("postback less ...!");
    <form id="form1" runat="server">
            <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" OnTextChanged="TextBox2_TextChanged"></asp:TextBox>
            <br />
            <br />
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
            <br />
            <br />
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" Text="" EnableViewState="true" ViewStateMode="Enabled"></asp:Label>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" />

when i change value of label in code behind , after postback value of label not change and i can see it . but when i change value of label with jquery , after postback value of label set be empty … why ? how can i fix it ?

Reason for this behaviour is, when you changes the value of label from code behind then server knows that there is a chnages in state of that label, show on post back it save the current state of label on view state.

But when you chnage sthe value of label on client end i.e using java script, server doesn’t know that there is any state change in your label, so the state of label become same as time of page load so in at the time of post back is save the same value of
label on view state which is at the time of page load.

I hope this will clear you confusion.

the browser does not post label values. when rendering the page, webforms writes the label value to the __veiwstate hidden field. this field is encluded in the postback. thats how webforms knows the label value on postback. there is no way for javascript
to update the __viewstate (it encrypted), so the best you can do is create a new hidden field you place the label value in.

Leave a Reply