Mohammad Fajar
Mohammad Fajar

Reputation: 1007

Change Variable Value with Asp.Net Button Click event

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.

Upvotes: 0

Views: 7481

Answers (2)

Tummala Krishna Kishore
Tummala Krishna Kishore

Reputation: 8271

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  ; 
        }

Upvotes: 2

Canvas
Canvas

Reputation: 5897

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

Upvotes: 0

Related Questions