簡體   English   中英

組合框中的靜態插入的項目在單擊按鈕時會翻倍不止一次

[英]The statically inserted items in combobox doubles on button click for more than once

我有一個已插入以下項目的組合框

public void SetOperationDropDown()
    {
    //ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS-.
    cmbOperations.SelectedItem = "-SELECT OPERATIONS-";

    //This is for adding four operations with value in operation dropdown
    cmbOperations.Items.Insert(0, "PrimaryKeyTables");
    cmbOperations.Items.Insert(1, "NonPrimaryKeyTables");
    cmbOperations.Items.Insert(2, "ForeignKeyTables");
    cmbOperations.Items.Insert(3, "NonForeignKeyTables");
    cmbOperations.Items.Insert(4, "UPPERCASEDTables");
    cmbOperations.Items.Insert(5, "lowercasedtables");
    }

但是,當用戶單擊按鈕不止一次時,該值就會翻倍,或者該值發生任何不必要的變化。

點擊按鈕是

private void btnConnect_Click(object sender, EventArgs e)
    {
    //Function call for validating the textboxes entry
    ValidateForm();

    //Variable to store server address
    string localHost = "192.168.10.3";

    //Variable to store userId and password of the database
    string logInDetails = "gp";

    try
        {
        //Checking for the Valid entries in textboxes if all entries are correct then call functions accordingly
        if((txtPassword.Text == logInDetails) && (txtUsername.Text == logInDetails) && (txtHost.Text == localHost))
            {

            //If connected then give this message to user
            lblMessage.Visible = true;
            lblMessage.Text = "You are connected to the SQL Server....";

            if(lblMessage.Text != string.Empty)
                {
                //Function call for binding the dropdown with all DB names 
                BindDBDropDown();

                //Function call for binding the operation names in dropdown 
                SetOperationDropDown();

                }
            }
        else
            {
            //Else give the error message to user
            lblMessage.Text = "Invalid Credentials";
            }
        }
    catch(Exception ex)
        {
        //All the exceptions are handled and written in the EventLog.
        EventLog log = new EventLog("Application");
        log.Source = "MFDBAnalyser";
        log.WriteEntry(ex.Message);
        }
    }

誰能幫我嗎?

public void SetOperationDropDown()
{
if(CmbOperations.Items.Count == 0)
{
//ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS-. 
cmbOperations.SelectedItem = "-SELECT OPERATIONS-"; 
//This is for adding four operations with value in operation dropdown 
cmbOperations.Items.Insert(0, "PrimaryKeyTables"); 
cmbOperations.Items.Insert(1, "NonPrimaryKeyTables"); 
cmbOperations.Items.Insert(2, "ForeignKeyTables"); 
cmbOperations.Items.Insert(3, "NonForeignKeyTables"); 
cmbOperations.Items.Insert(4, "UPPERCASEDTables"); 
cmbOperations.Items.Insert(5, "lowercasedtables"); 


}
else
{
int? cbSelectedValue = null;
if(!string.IsNullOrEmpty(cmbOperations.SelectedValue))
cbSelectedValue = convert.toInt32(cmbOperations.SelectedValue);
}
//load your combo again
if(cbSelectedValue != null)
cmbOperations.SelectedValue = cbSelectedValue.ToString();
}

由於我沒有使用VS,所以可能會有一些小的語法錯誤。

僅當頁面的加載不是postback時才調用SetOperationDropDown()

 if (!IsPostBack) {
    SetOperationDropDown();
 }

用戶單擊時禁用該按鈕,將cmbOperations SelectItem重置為“不執行任何操作”值,並在完成請求處理后重新啟用該按鈕。

這是在WinForms下標記的,因此我認為回發不適用於此處。 在您的btnStartAnalysis_Click方法中,我看不到它調用SetOperationDropDown 嘗試進入DEBUG模式並在SetOperationDropDown放置一個斷點。 然后單擊您的按鈕,查看是否達到了斷點。 如果是這樣,則請參考您的堆棧跟蹤以查看從何處SetOperationDropDown

如果WinForms標記不正確,而您實際上正在使用WebForms / ASP.NET,請執行Stefanvds和Marcel的建議。 但是我認為弄清楚SetOperationDropDown從哪里被錯誤地調用很重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM