【问题标题】:Bool property is set to true without being calledBool 属性设置为 true 而不被调用
【发布时间】:2012-06-19 16:01:54
【问题描述】:

我正在制作一个用于阅读文章的网络应用程序。

如果单击特定的Edit 按钮,我有设置为true 的bool 属性,还有一个Save 按钮,在填写TextBoxes 和其他控件的用户信息后单击。每当我运行我的应用程序并填写信息时,它运行良好,但在运行 4 或 5 次(重新启动应用程序)后,单击 Save 按钮时会出现异常错误: Input string was not in a correct format 这是由于用 Null 填充 TextBoxes(首先将 Null 转换为 Int)。

我的问题是 bool 属性 (Managment.IsEditing) 无缘无故设置为 true(应按下 Edit 按钮将 bool 设置为 true)。为什么以及如何自动设置为 true,因为它的代码仅在 EditButton_Click 事件中被调用?

这是代码

protected void EditButton_Click(object sender, EventArgs e)
{
    if(EditorList.SelectedIndex > -1) //Just to ensure that Item is selected from ListBox
    {   
        //editor is the poco  , EditorManager is the editor table manager
        editor = EditorManager.GetEditorInfo(EditorList.SelectedValue);

        NameTextBox.Text = editor.Name;
        EmailTextBox1.Text = editor.Email;
        PasswordTextBox.Text = editor.Password;
        EditorIDTextBox.Text = editor.Editor_ID.ToString();

        for (int index = 0; index < RoleCheckBoxList.Items.Count; index++)
        {
          RoleCheckBoxList.Items[index].Selected = editor.RoleList[index];
        }

        Managment.IsEditing = true; //This flag is responsible for telling "SaveButtton" that editor would be updated. 
        DeleteButton.Enabled = true;
        ResultLabel.Text = "";
    }

    else
        ResultLabel.Text = "Select Editor from list first";

protected void SaveButton_Click(object sender, EventArgs e)
{
    if(Managment.IsEditing == false) //it makes sure that new editor is being saved
    {
        editor.Name = NameTextBox.Text;
        string email = EmailTextBox1.Text;
        editor.Email = email.ToLower();
        editor.Password = PasswordTextBox.Text;

        if(EditorManager.IsEditorValid(editor.Email))
        {
            for (int index = 0; index < RoleCheckBoxList.Items.Count; index++)
            {
              editor.RoleList[index] = RoleCheckBoxList.Items[index].Selected;
            }

            EditorManager.Save(editor);
            ResultLabel.Text = editor.DataUploadMessage;            
        }

        else
            ResultLabel.Text = "Editor with the same Email can't be add!";

        FillEditorList();
    }

    else if(Managment.IsEditing == true) //it determines that existing editor is being updated and problem is that Managment.IsEditing is turned on without being called.
    {
        editor.Name = NameTextBox.Text;
        string email = EmailTextBox1.Text;
        editor.Email = email.ToLower();
        editor.Password = PasswordTextBox.Text;
        editor.Editor_ID = Convert.ToInt32(EditorIDTextBox.Text);// Error occurs at this line

        for (int index = 0; index < RoleCheckBoxList.Items.Count; index++)
        {
          editor.RoleList[index] = RoleCheckBoxList.Items[index].Selected;
        }

        EditorManager.Edit(editor);

        ResultLabel.Text = editor.DataUploadMessage;
        Managment.IsEditing = false;
        FillEditorList();            
    }
    ClearFields(Form.Controls);
}

该行发生异常:

editor.Editor_ID = Convert.ToInt32(EditorIDTextBox.Text);

给各位小伙伴带来不便深表歉意

【问题讨论】:

  • 请给我们问题的代码。
  • 请给我们一些代码
  • 为什么初学者被否决,给他们一些时间来学习 SO 环境!
  • 计算机不只是在搞砸你,这里只有两种可能性:要么你有代码导致点击该按钮(客户端脚本可能模拟点击?)或者有在代码中调用 EditButton_Click 方法(或设置属性)的位置。
  • “运行 4 或 5 次后”是什么意思?什么是跑步?是否运行单击编辑然后保存?还是运行实际启动了应用程序?

标签: c# asp.net visual-studio-2010 boolean


【解决方案1】:

当指定行发生异常时,重置标志的行将不会运行。该页面可能会处于无效状态。

尝试修复/处理错误并正确清理,问题可能会消失。

【讨论】:

  • 我使用 Try catch 来处理异常,它不会让我的应用程序崩溃,但它并没有解决我的问题。当您为重置标志的行不会运行而感到难过时......所以我的问题是为什么这个标志在没有被调用的情况下变为真。 (此标志在“EditButoon_Click”事件中变为真)
  • 我非常仔细地编辑我的帖子并尽力解释。我要求再读一遍。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2010-11-10
  • 2013-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-20
  • 1970-01-01
相关资源
最近更新 更多