[英]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.