简体   繁体   中英

Getting updated value of Textbox in asp.net and c#

I am searching and posting a lot in this issue,but could not get the satisfactory answer.I wanted to make sure that whenever I am making a call to server the value of Textbox should be the one which is the most updated one.

But when I am making the call to server the old value of the textbox persists.I know it is something to do with the postback,but I am not getting the exact way to get the updated value of textbox in my .cs file.

Please tell me what are the necessary steps that I should take to always get the latest value of the textbox.

Here is my code which is not working:

protected void Page_Load(object sender, EventArgs e)
    {


        int contentID = Convert.ToInt32(Request.QueryString["contentid"]);
        if (contentID != 0)
        {
                if (!this.IsPostBack)
                {
                getContentBody(contentID);
                TextBox1.Text = content;
                msg_lbl.Text="Inside not PostBack";
                }
                else
                {
                getContentBody(contentID);
                TextBox1.Text = content;
                msg_lbl.Text="Inside PostBack";
                }
            }


            else
                Response.Write("Invalid URL for article");


    }



 public void getContentBody(int contentID)
    {
        try
        {
            //////////////Opening the connection///////////////

            mycon.Open();
            string str = "select content from content where contentID='" + contentID + "'";
            //Response.Write(str);
            MySqlCommand command1 = mycon.CreateCommand();
            command1.CommandText = str;
            dr = command1.ExecuteReader();
            if (dr.Read())
            {
                content = dr[0].ToString();
            }
        }
        catch (Exception ex)
        {
            Response.Write("Exception reading data" + ex);
        }
        finally
        {
            dr.Close();
            mycon.Close();
        }
    }

 protected void Button2_Click(object sender, EventArgs e)
    {
            //string textboxvalue = Request.Form[TextBox1.UniqueID];

            mycon.Open();
            string query = "update content set content='" +TextBox1.Text + "' where contentID= '"+contentID +"'";
            msg_lbl.Text = query;
            try
            {
                MySqlCommand command1 = mycon.CreateCommand();
                command1.CommandText = query;
                command1.ExecuteNonQuery();
                msg_lbl.Text = "text" + TextBox1.Text;
            }
            catch (Exception ex)
            {
                msg_lbl.Text = "Exception in saving data" + ex;
            }
            finally
            {
                mycon.Close();

            }


    }

Here is my aspx page code:

      <asp:TextBox ID="TextBox1" runat="server" Height="500px" 
             TextMode="MultiLine" Width="90%" AutoPostBack="True"></asp:TextBox>
    </p>
    <p>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Delete post" />
        &nbsp;
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
            Text="Save changes" />
&nbsp;
        <asp:Button ID="Button3" runat="server" Text="Cancel" />
    </p>

Please also tell me the reason why it is not working and how I can make it work.

Thanks,

Amandeep

According to the ASP.NET's life cycle, Page_Load executes before Button2_Click, so you have to show your updated text in the Button2_Click:

protected void Page_Load(object sender, EventArgs e)
{


    contentID = Convert.ToInt32(Request.QueryString["contentid"]);
    if (contentID != 0)
    {
        if (!this.IsPostBack)
        {
            getContentBody(contentID);
            TextBox1.Text = content;
            msg_lbl.Text = "Inside not PostBack";
        }
    }
    else
        Response.Write("Invalid URL for article");
}

public void getContentBody(int contentID)
{
    try
    {
        //////////////Opening the connection///////////////

        mycon.Open();
        string str = "select content from content where contentID='" + contentID + "'";
        //Response.Write(str);
        MySqlCommand command1 = mycon.CreateCommand();
        command1.CommandText = str;
        dr = command1.ExecuteReader();
        if (dr.Read())
        {
            content = dr[0].ToString();
        }
    }
    catch (Exception ex)
    {
        Response.Write("Exception reading data" + ex);
    }
    finally
    {
        dr.Close();
        mycon.Close();
    }
}

protected void Button2_Click(object sender, EventArgs e)
{
    //string textboxvalue = Request.Form[TextBox1.UniqueID];

    mycon.Open();
    string query = "update content set content='" + TextBox1.Text + "' where contentID= '" + contentID + "'";
    msg_lbl.Text = query;
    try
    {
        MySqlCommand command1 = mycon.CreateCommand();
        command1.CommandText = query;
        command1.ExecuteNonQuery();
        getContentBody(contentID);
        TextBox1.Text = content;

        msg_lbl.Text = "text" + TextBox1.Text;
    }
    catch (Exception ex)
    {
        msg_lbl.Text = "Exception in saving data" + ex;
    }
    finally
    {
        mycon.Close();

    }


}

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