1

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

6
  • Can you post your aspx markup and some sample data? Commented Oct 31, 2011 at 5:03
  • Tried setting breakpoints and debug the code? Commented Oct 31, 2011 at 5:17
  • Hi greg,I added the aspx code,but as sample data please tell me what you are expecting? Commented Oct 31, 2011 at 5:29
  • Button2_Click click event how contentid is passing there.. its not public variable! Make it as public variable or get Request querystring again! Commented Oct 31, 2011 at 5:36
  • Thanks anand for marking this bug,as I also declared contentid as global variable it was not giving me any error ,but there was that bug which I changed.But still the problem is not solved for the textbox. Commented Oct 31, 2011 at 5:58

1 Answer 1

1

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();

    }


}
Sign up to request clarification or add additional context in comments.

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.