2010-08-04 51 views
1

使用C#& Mysql的当我按下按钮组合框的值没有显示

我有组合框在我的网页&按钮,如果我选择从组合框的值,然后我点击按钮的页面令人耳目一新,组合框价值在变化。

对于实例

Combobox value: 1, 2, 3, 4 ..... 

If i selected 2 then i press the button, webpage is refreshing, combobox values is display from: 1, 2, 3, 4.... 

Combobox should display 2 

Why combobox is displaying a selected value when i press the button. 

表名:用户

Name username 

Raja 1 
Ravi 2 
Ram 3 
kumar 4 
..., 

按钮的Click事件代码

cmd1 = new OdbcCommand("Select name from users where username = '" + combobox1.Items[combobox1.SelectedIndex].Text + "' ", dbcon); 
      dr1 = cmd1.ExecuteReader(); 
      while (dr1.Read()) 
      { 
       textbox1.Text = dr1.GetString(0); 

      } 
      dr1.Close(); 

上面的代码工作,但如果我在选择值2组合框,然后我按下按钮文本框应显示镭射,但文本框显示王侯,也combobo x还令人耳目一新,组合框不显示1而不是2.

如何解决此问题。

需要C#代码帮助

+0

可能需要更具体的代码那么这个sudo代码可以帮助你找出问题所在。 – Jim 2010-08-04 13:12:02

回答

1

Understanding ASP.NET View State可能是一个有用的阅读。通常,您需要以某种方式存储输入的值,然后在页面加载时恢复。

鉴于按钮重新加载整个页面的事实,重新加载它的一部分不是更有效吗?

0

只需使用 combobox1.SelectedIndex或SelectedText

,而不是你所使用的。可能是它解决您的问题

0

也许你正在填充在页面加载的下拉列表,所以一定要填充下拉列表中!Page.IsPostBack相反,我认为这是你的问题