简体   繁体   中英

persistent text boxes in c# asp.net

I fill the textboxes on page_load event.Then I edit textboxes data and tried to update data. Variables are assigned with old values. How can I do to get the new values.
Here is my code behind

using System;
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.Configuration;  
using System.Data;  
using System.Data.SqlClient;  

public partial class mymembertype : System.Web.UI.Page
{
    public static int mem_typeid;

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["valueid"] != null)
    {
        mem_typeid = (int)(Session["valueid"]);
        string memtype_name = Convert.ToString(Session["valueName"]);
        string rate = Convert.ToString(Session["rate"]);
        txtmembtype.Text = Convert.ToString(memtype_name);
        txtdscrate.Text = rate;
        Insert_membertype.Text = "Update";
    }
    }

protected void Insert_membertype_Click(object sender, EventArgs e)
{

    funtions fun = new funtions();


        if (txtmembtype.Text != "" && txtdscrate.Text != "" )
        {
            if (Insert_membertype.Text == "Save")
            {   
                string membetype = txtmembtype.Text;
                int dscrate = Convert.ToInt32(txtdscrate.Text);

                bool chk = fun.Insert_membertype(membetype, dscrate);
                if (chk)
                    lblInfo.Text = " saving membertype successful";
                else
                    lblInfo.Text = "Error saving membertype";

        }

    else
    {

                string membetype = txtmembtype.Text;
                int dscrate = Convert.ToInt32(txtdscrate.Text);

                bool chk = fun.Update_memberType(mem_typeid, membetype, dscrate);
                if (chk)
                    lblInfo.Text = " Updating membertype successful";
                else
                    lblInfo.Text = "Error Updating membertype";
            }
        }


    }


}  

As you see second condition block is for updating data. But it have only values in page load.Now new data is assigned. Please ....

Your problem is that Page Load is called before the event handler for your controls. If you want to understand more about the order things happen in ASP.net just Google "ASP.Net Page Lifecycle" - if you're going to be doing a lot of development I recommend at least getting a basic understanding of what ASP.Net is doing.

You'll want to change your page load to check if the current request is a postback.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack && Session["valueid"] != null)
    {
        // Doing stuff
    }
}

The "IsPostBack" variable is false the first time the page is loaded, and then is true for every subsequent load.

在Pageload事件中尝试if(!IsPostBack)方法

You need to write the code inside page_load under IsPostback condition like below to get the updated values

if (!IsPostBack)
        { 
           // Bind your code here 
        }

write a code GetData Method.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetData();


            }
        }
        public void GetData()
        {

        }

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