简体   繁体   中英

javascript Changes clear after postback

Here I am posting code,

ASP:

   <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
        CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
        <script type="text/javascript">

        function ShowHideBox() {
            boxdl.style.display = 'none';
        }
    </script>
    </asp:Content>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        First Value:<asp:TextBox ID="first" runat="server" ></asp:TextBox><br />
        Second Value:<asp:TextBox ID="second" runat="server" ></asp:TextBox><br />
        Calculation:<asp:TextBox ID="calc" runat="server" ></asp:TextBox><br />
        <asp:Button ID="Calculate" runat="server" Text="Calculate" 
            onclick="Calculate_Click" />
         <div onclick="ShowHideBox();" style="display:block;height:10px;width:100px;background-color:#258000;"></div>
        <div style="background-color:#852000;width:100px;height:100px;display:block;border:black 1px solid;" id="boxdl">

        </div>
    </asp:Content>

C# Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Calculate_Click(object sender, EventArgs e)
    {
        calc.Text = (int.Parse(first.Text) + int.Parse(second.Text)).ToString();
    }
}

when I click on green box, redbox disappears, but when I click on calculate button, it calculate result of two text boxes and shows result in third textbox, but the problem is that the redbox appears after occurrence of postback event, why??? is there any solution to prevent this problem??? this is just a demo. this thing is used lots of times in my project...

This is the expected output. Since you posting back to the server, the JS and the state of HTML controls will not be persisted.

One solution can be to make these controls server side, ie runat="server".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM