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

    Control looses value set in javascript function on server side event

    In javascript function I checking checkbox and setting value to label after satisfication of perticular condition.But in serverside event I am getting checkbox1.checked = false and text of label is default text of that label, not modified one.Why this is happening?How to handle that?
  • #766401
    Hi
    Ofcourse

    Whether in our aspx Page is postback we canot track the client side value.

    defiantly looses value in javascript because javascript is client side script .

    Page post event firing server side so that situation page re rendering so we canot capture the client side value

    try to use server side then using ajax panel thats better for you.

    for ex : try this code


    <asp:ScriptManager ID="ScriptManager"
    runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1"
    UpdateMode="Conditional"
    runat="server">
    <ContentTemplate>
    <fieldset>
    <legend>UpdatePanel content</legend>
    <!-- Other content in the panel. -->
    <%=DateTime.Now.ToString() %>
    <br />
    <asp:Button ID="Button1"
    Text="Refresh Panel"
    runat="server" />
    </fieldset>
    </ContentTemplate>
    </asp:UpdatePanel>

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766402
    if you change the value of this control through javascript in the client side it will not be propagated via postback in the serverside...whatever you do with javascript unless you remove readonly="true". Now there is a solution to this problem as described in the article above.

    Simply put this in the PageLoad event

    if (!IsPostBack)
    txtName.Attributes.Add("readonly","readonly");

    Just don't forget to remove ReadOnly="true" or Enable="false" if your intent was to disable the control from editing just use the snippet above. Don't forget to remove Enable="false" if you put it on.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766407
    Hi,

    I guess using HiddenField you can achieve your task, instead of Label control try to use hidden field that was helpful to you. Apart from the above try to overcome postback in a page by using AJAX UpdatePanel.


    <asp:UpdatePanel ID="upCancel" runat="server" UpdateMode="Always">
    <ContentTemplate>
    <asp:ImageButton ID="btnCancelYes" OnClick="btnCancelYes_Click" runat="server" ImageUrl="~/Images/btnyes.jpg" />
    </ContentTemplate>
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="btnCancelYes" EventName="Click" />
    </Triggers>
    </asp:UpdatePanel>



    Hope this will helpful to you....

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments