我有一个组合框,其中插入了以下项目
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);
}
}
任何人都可以帮助我吗?
答案 0 :(得分:1)
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,可能会出现语法错误。
答案 1 :(得分:0)
仅在页面加载不是SetOperationDropDown()
postback
if (!IsPostBack) {
SetOperationDropDown();
}
答案 2 :(得分:0)
当用户点击它时禁用该按钮,将cmbOperations SelectItem重置为“不执行任何操作”值,在处理完请求后重新启用该按钮。
答案 3 :(得分:0)
这是在WinForms下标记的,所以我不认为帖子在这里适用。查看btnStartAnalysis_Click
方法,我认为它不会调用SetOperationDropDown
。尝试进入DEBUG模式并在SetOperationDropDown
中设置一个断点。然后单击您的按钮,查看您的断点是否被击中。如果是,则参考堆栈跟踪以查看调用SetOperationDropDown
的位置。
如果WinForms标记不正确并且您实际上正在使用WebForms / ASP.NET,那么请执行Stefanvds和Marcel建议的内容。但我认为找出SetOperationDropDown
被错误调用的地方很重要。