使用DropDownList中选择的数据填充TextBox1 [英] Populating TextBox1 with data selected in DropDownList
问题描述
如何填充 我双击DropDownList1并发现SelectedIndexChanged是附加到该对象的方法。 跨度>
TextBox1
with the value在
中选择 DropDownList1好吗?
-
但是,当我运行网站并更改值时 - 没有任何反应
-
当我添加一个按钮并在按钮中使用相同的代码时,它的工作原理,问题是我需要点击按钮,
然后页面刷新,这就是什么我想避免。我只想在DropDownList1中更改值时获得相同的效果
跨度>
跨度>
跨度>
您好btsDeveloper,
你需要使用"SelectedIndex"的"SelectedItem"。请查看下面的代码。希望这对你有帮助。
使用System;
使用System.Windows.Forms;
namespace ComboBox_To_TextBox
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
{
textBox1.Text = comboBox1.SelectedItem .ToString();
}
}
}
输出:
How do I populate a
TextBox1
with the value selected in
DropDownList1 please ? I double clicked on the DropDownList1 and noticed that SelectedIndexChanged is the method attached to that object.
But, when I run the website and change the value - nothing happens
When I add a button and use the same code in the button, then it works, the problem is I need to click the button,
then the page is refreshing and this is what I want to avoid. I would just like to get the same effect when I change the value in the DropDownList1
Hi btsDeveloper,
You need to use "SelectedItem" insted of "SelectedIndex". Please look into the below code. Hope this helps you.
using System; using System.Windows.Forms; namespace ComboBox_To_TextBox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { textBox1.Text = comboBox1.SelectedItem.ToString(); } } }Output:
这篇关于使用DropDownList中选择的数据填充TextBox1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!