Reputation:
I have a textbox "txtName" listbox "listNames" and button "btn_Add". Now i want to populate the textbox with the name selected from the listbox on button click.
I am using asp.net and c#. please help.
Upvotes: 0
Views: 2684
Reputation: 63105
do as below
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//load listbox items here
}
}
if you load data on page load, in every post back your listbox will load again and again, you will lost the selection. Do as above to load data only first time page load. now you can get the list box selected items in button click event.
Upvotes: 1
Reputation: 12630
Put this on your code behind
protected void Page_Load(object sender, EventArgs e)
{
listNames.Items.Add("listboxItemValue1","Listbox Item Text 1");
listNames.Items.Add("listboxItemValue2","Listbox Item Text 2");
}
protected void btn_Add_Click(object sender, EventArgs e)
{
txtName.Text = listNames.SelectedItem.Text;
}
and this on your Asp,net page
<asp:button ID="btn_Add" runat=server" OnClick="btn_Add_Click" />
Upvotes: 0
Reputation: 778
Try this,
On code behind button click event.
txtText.Text = drpDwn.SelectedValue; // This is for ID
txtText.Text = drpDwn.SelectedItem.Text; //This is for Text
Upvotes: 0
Reputation: 3469
But this in your button.
txtName.Text = listNames.SelectedItem.Text;
Upvotes: 0