C# 在asp.net和c中获取文本框的更新值#

C# 在asp.net和c中获取文本框的更新值#,c#,asp.net,C#,Asp.net,我在这个问题上搜索和发布了很多信息,但是没有得到满意的答案。我想确保每当我打电话给服务器时,Textbox的值应该是最新的值 但是当我调用服务器时,textbox的旧值仍然存在。我知道这与回发有关,但我没有得到在.cs文件中获取textbox更新值的确切方法 请告诉我应该采取哪些必要步骤来始终获取文本框的最新值 以下是我的代码,它不起作用: protected void Page_Load(object sender, EventArgs e) { int conte

我在这个问题上搜索和发布了很多信息,但是没有得到满意的答案。我想确保每当我打电话给服务器时,Textbox的值应该是最新的值

但是当我调用服务器时,textbox的旧值仍然存在。我知道这与回发有关,但我没有得到在.cs文件中获取textbox更新值的确切方法

请告诉我应该采取哪些必要步骤来始终获取文本框的最新值

以下是我的代码,它不起作用:

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

            }


    }
以下是我的aspx页面代码:

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

还请告诉我它不起作用的原因以及我如何使它起作用

谢谢


Amandeep

根据ASP.NET的生命周期,页面加载在按钮2单击之前执行,因此您必须在按钮2单击中显示更新的文本:

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

    }


}

根据ASP.NET的生命周期,页面加载在按钮2单击之前执行,因此您必须在按钮2单击中显示更新的文本:

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

    }


}

您可以发布您的aspx标记和一些示例数据吗?尝试设置断点并调试代码?您好,greg,我添加了aspx代码,但作为示例数据,请告诉我您期望的是什么?按钮2\u单击事件contentid如何在那里传递。。它不是公共变量!将其设为公共变量或再次获取请求查询字符串!感谢anand标记此错误,因为我还将contentid声明为全局变量,它没有给我任何错误,但我更改了该错误。但文本框的问题仍然没有解决。你能发布你的aspx标记和一些示例数据吗?尝试设置断点并调试代码吗?嗨,greg,我添加了aspx代码,但作为示例数据,请告诉我您期望的是什么?按钮2\u单击事件contentid是如何传递到那里的。。它不是公共变量!将其设为公共变量或再次获取请求查询字符串!感谢anand标记这个bug,因为我还声明contentid为全局变量,它没有给我任何错误,但是有一个bug是我更改的。但是对于textbox,问题仍然没有解决。