Click here to Skip to main content
14,242,633 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I am using flag variable to identify add operation or update operation in the same button.
Depending on the flag value it will perform add or update.

But whenever I click a button in asp page, flag value will be reinitialized.
I have used in page load (!Page.Ispostback). Please help me out.

Thanks in advance.

protected void Button1_Click(object sender, EventArgs e)
    {
        if (addupflag == 1)
        {
            Button1.Text = "Add";
            if ((TextBox1.Text == "") || (TextBox2.Text == ""))
            {
                Label4.Text = "User name and password can't be empty";
                TextBox1.Focus();
            }
            else
            {
                int cnt = ListBox1.Items.Count;
                dt1.Clear();
                dt1 = g1.Addusers(TextBox1.Text.ToString(), TextBox2.Text.ToString(), DropDownList1.SelectedValue);
                LoadAddresource(dt1);
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox1.Focus();
            }
        }
        else
        {
            dt1.Clear();
            dt1 = g1.Upadteusers(TextBox1.Text.ToString(), TextBox2.Text.ToString(), DropDownList1.SelectedValue);
            LoadAddresource(dt1);
            Button2.Enabled = false;
            TextBox1.Text = "";
            TextBox2.Text = "";
            Button1.Text = "Add";
            addupflag = 1;
            TextBox1.Enabled = true;
        }
    }

this is the code after update its value should be 1 but it'l show "0"
Posted
Updated 21-Nov-10 22:32pm
v3
Comments
Sandeep Mewara 22-Nov-10 4:25am
   
Post code. Until unless you don't put the code for it, it should not get reinitialized.
shwetha_m6 22-Nov-10 4:30am
   
[EDIT]

I moved the code to the main Question so as to get the Code Block Formatting.
Dalek Dave 22-Nov-10 4:31am
   
Edited for Readability.
Rate this:
Please Sign up or sign in to vote.

Solution 1

You must save the flag value in session or in a hiddenfield.
   
Comments
shwetha_m6 22-Nov-10 4:22am
   
u mean to say in a public variable..? will u please tell me how to do that..!!
Sebastien T. 22-Nov-10 4:33am
   
no, in a asp page. You add <asp:HiddenField id="myFlag" value="1" runat="server" /> and in your .cs, you can have if myFlag.value == "1". A another solution is add your flag in the viewstate (http://msdn.microsoft.com/fr-fr/library/system.web.ui.control.viewstate%28VS.80%29.aspx)

You lost the value because in ASP.NET variable is destroyed after the page has done running.
shwetha_m6 22-Nov-10 5:05am
   
thanks dude.. i've used public static flag its workin fine now..
Rate this:
Please Sign up or sign in to vote.

Solution 2

You could use the flag as part of the application state.
   
Comments
shwetha_m6 22-Nov-10 7:03am
   
Sorry this i dint get..!!
Rate this:
Please Sign up or sign in to vote.

Solution 3

Why dont you just declare the flag variable as a private global variable for the form, and initialize it value in the page load section under the
if(!Page.IsPostBack) property. This way, the flag will not be reinitialized on the calling of the button_click event. I hope you understand, what I am trying to tell.
   
Comments
shwetha_m6 22-Nov-10 7:03am
   
This doesn't work i've checked it..!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100