【问题标题】:Disabled asp.net Text box return old value after enabling and changing text启用和更改文本后禁用的 asp.net 文本框返回旧值
【发布时间】:2011-05-07 18:36:55
【问题描述】:

我的问题是我有一个 asp 文本框,它有一个值(文本)但已禁用(启用 =“假”)

点击(编辑)按钮后,它会启用(启用=“真”),所以我可以更改文本

--> 更改文本并单击(保存)按钮后,它采用之前的旧值(文本) 编辑并保存它而不是新的..

ReadOnly 属性也会出现此问题。

我被困在这个..所以任何帮助我都会感激不尽。

    <telerik:RadAjaxPanel ID="AjaxPanel1" runat="server" LoadingPanelID="LoadingPanelTelerik">
<tr>
  <td align="right" valign="top">
      <asp:TextBox runat="server" ID="Email" CssClass="InputForm" /> 
  </td>
  </tr>
<tr>
  <td align="center" colspan="2" valign="bottom">
      <asp:Button ID="UpdateButton" runat="server" Text="تـعــديـل" OnClick="Enable_Info" Font-Size="Large" />
      <asp:Button ID="CancelButton" runat="server" Text="إلـغـاء" OnClick="Cancel_Info" Font-Size="Large" Visible="false"/> 
  </td>        
</tr>
</telerik:RadAjaxPanel>

这是 C# 代码(不需要编写 SQL 连接):

 protected void Page_Load(object sender, EventArgs e)
{
    Email.Text = myDataTable.Rows[0][2].ToString();
    Email.DataBind();

    Email.Enabled = false;

}

    protected void Enable_Info(object sender, EventArgs e)
{
    if (UpdateButton.Text == "تـعــديـل")
    {


        Email.Enabled = true;

        UpdateButton.Text = "حــفـظ";

        CancelButton.Visible = true;

    }
    else if (UpdateButton.Text == "حــفـظ")
    {

        AjaxPanel1.EnableAJAX = false;

        int CustomerId = System.Convert.ToInt32(Request.Cookies["User"]["CustomerId"]);
        string email = Email.Text;

        SqlConnection conn = new SqlConnection(ArabShoppingConfiguration.DbConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update Customer set Email = '" + email + "' where Id = '" + CustomerId + "'";

        try
        {
            conn.Open();
            cmd.ExecuteScalar();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
        }

    }
}

protected void Cancel_Info(object sender, EventArgs e)
{

    Email.Enabled = false;

    UpdateButton.Text = "تـعــديـل";

    CancelButton.Visible = false;
}

【问题讨论】:

  • 你能把相关的标记和代码贴在后面吗?
  • 顺便说一句,永远不要使用throw ex;。它搞砸了堆栈跟踪。对于SqlConnection,您应该只使用using 块。
  • 感谢您的建议...第一次知道

标签: c# asp.net ajax


【解决方案1】:

好的,所以问题是您在页面加载中设置 Email.Text,每次加载页面时都会运行该页面以包含所有回发。页面加载在 Enable_Info 方法之前触发,因此它每次都将文本更改回来。您需要做的就是修改您的 page_load 代码,使其仅在页面第一次加载时运行,而不是在回发时运行:

protected void Page_Load(object sender, EventArgs e)
{
  if(!Page.IsPostBack)
  {
    Email.Text = myDataTable.Rows[0][2].ToString();
    Email.DataBind();

    Email.Enabled = false;
  }
}

【讨论】:

  • 非常感谢..你救了我
猜你喜欢
  • 1970-01-01
  • 2014-10-25
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
  • 2016-11-20
  • 2010-11-28
  • 1970-01-01
  • 2019-02-16
相关资源
最近更新 更多