禁用的asp.net文本框在启用和更改文本后返回旧值

时间:2011-05-07 18:36:56

标签: c# asp.net ajax

我的问题是我有一个asp文本框,它有一个值(文本)但是被禁用(Enabled =“false”)

点击(编辑)按钮后,它变为启用状态(启用=“真”),这样我就可以更改文字了

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

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

1 个答案:

答案 0 :(得分:4)

好的,问题是您在页面加载中设置了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;
  }
}