0

I'am beginner in ASP.net. I recently created some Web Form Application with following code:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

        <div>
            <asp:Button ID="Button1" runat="server" Text="CHECK" OnClick="Check_Data" />
            <asp:Label ID="ketCekData" runat="server" Text="Label"></asp:Label>
        </div>

        <div>
            <asp:Button ID="Button2" runat="server" Text="ASSIGN" OnClick="Assign_Data"  />
            <asp:Label ID="labelProcess" runat="server" Text="Label"></asp:Label>
        </div>

    </form>
</body>
</html>

And code behind:

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

namespace TestAssignVariable
{
    public partial class MainForm : System.Web.UI.Page
    {

        private string file_path = "startx";

        int a = 12; 


        private string FilePath
        {
            get
            {
                return file_path; 
            }
            set
            {
                file_path = value; 
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Check_Data(object sender, EventArgs e)
        {
            ketCekData.Text = a.ToString()  ; 
        }

        protected void Assign_Data(object sender, EventArgs e)
        {
            // FilePath = "AWESOME"; 
            a = 100; 
            labelProcess.Text = "Data Assigned"; 
        }

    }
}

So there is two button with ID Button1 and Button2. When Button2 was clicked, then it's firing an event to change the value of variable a from 12 to 100. Button1 then displaying the value of variable a on label ketChekData. So when I click Button2 followed Button1 there must be 100 displayed in label ketCekData. But I dont understand why this is not worked: there still 12 displayed on label ketCekData.

10
  • 1
    you are assigning a value on global level so on every postback value is resetting to 12 Commented Oct 1, 2015 at 8:33
  • 1
    Take int a as static. Please read some blog related to Page life cycle. Value assigned to int a will get lost every time you postback. Use static to preserve changes but only do this at application level. Commented Oct 1, 2015 at 8:35
  • So what I must do? And can you give me some instant article to explain that? There is book about asp.net with 900 page in my PC. I can not browse that every page to find explanation. Commented Oct 1, 2015 at 8:36
  • you want that value(i.e. 12) to be used in another page Commented Oct 1, 2015 at 8:40
  • @Suprabhat - You should avoid using static variables in asp.net app. Commented Oct 1, 2015 at 8:40

2 Answers 2

2

The Value of a is declared outside the page_load .So on Every Postback value is Resetting. So create a hidden Field in Aspx

In Aspx

<asp:HiddenField ID="hdna" value="12" runat="server" />

In Cs

Remove int a=12;

 protected void Check_Data(object sender, EventArgs e)
        {
            ketCekData.Text = hdna.value  ; 
        }
Sign up to request clarification or add additional context in comments.

1 Comment

@Mohammad Fajar Instead a Now please use the hdna.value
0

Take a look at my dotnetfiddle here : https://dotnetfiddle.net/L8lUSY

Html

@model HelloWorldMvcApp.ViewModelExample
@{
    Layout = null;
}

<!DOCTYPE html>
<!-- template from http://getbootstrap.com/getting-started -->

<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Bootstrap 101 Template</title>

        <!-- CSS Includes -->
        <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

        <style type="text/css">

            .field-validation-error {
                color: #ff0000;
            }

        </style>
    </head>

    <body>
        <div class="container">
            <div class="col-md-6 col-md-offset-3">
                <h1>Create item</h1>
                <div class="form-group parentId">
                    @Html.LabelFor(m => m.ParentId)
                    @Html.TextBoxFor(m => m.ParentId)
                </div>
                <button class="button1">Button 1</button>
            <button class="button2">Button 2</button>
            </div>
        </div>

        <!-- JS includes -->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

        <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
        <script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script>

        <script type="text/javascript">


            $(function(){
                $('.parentId').hide();

                $('.button1').on('click', function()
                {
                    $('.parentId').toggle();
                });

                $('.button2').on('click', function()
                                 {
                                    $('#ParentId').val('100'); 
                                 });
            });

        </script>
    </body>
</html>

It may just be easier for you to use javascript / jQuery to change the value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.