Suppose we have two fields, the first one a Text Box showing an ID and a the second one a DropDownList with the name associated to this ID.
If you want the dropdownlist to change its value when the Text Box change his value also, you could do it in an programatically way
ListItem aValue = ddlDeportes.Items.FindByValue(txbxDeporte.Text);
ddlDeportes.SelectedIndex = ddlDeportes.Items.IndexOf(aValue);
It´s important to configure the dropdownlistconnecting the value Field to the ID, and the text field to the name which is showed by the dropdownlist.
In the first line we search into the dropdownlist using the value showed in the text box. Then we obtain a ListItem object containing the object to be selected into the dropdownlist. Finally using IndexOf we select this item in the dropdownlist
On the other way, if you want to change the value in the TextBox when you select a value from the dropdownlist then, you have to use the event OnSelectedIndexChanged for the dropdownlist, and use the following code.
txbxDeporte.Text = Convert.ToString(ddlDeportes.SelectedValue);
Is important also to enable the AutoPostBack property for the dropdownlist. Then the mark-up code for this component would be as follows:
On the other way, if you want to change the value in the TextBox when you select a value from the dropdownlist then, you have to use the event OnSelectedIndexChanged for the dropdownlist, and use the following code.
txbxDeporte.Text = Convert.ToString(ddlDeportes.SelectedValue);
Is important also to enable the AutoPostBack property for the dropdownlist. Then the mark-up code for this component would be as follows:
ddlDeportes_GetData would be the method used to populate the dropdownlist using LINQ to get the data:
public IQueryable ddlDeportes_GetData()
{
var db = new ClubSiteContext();
IQueryable query = from sports in db.Sports
orderby sports.Name
select sports;
return query;
}
{
var db = new ClubSiteContext();
IQueryable
orderby sports.Name
select sports;
return query;
}
No hay comentarios:
Publicar un comentario