Where Set ListBox Selected index if inside a FormView?
Where in the event li开发者_如何学Cfe cycle should I set the ListBox.SelectedIndex if the Listbox is contained within a FormView? What I'm trying to do is increase the SelectedIndex by 1 which makes it move from item to item whenever a user clicks on a submit button.
You have to use FindControl to access the listbox and then increment the value. The following code would go in the button submit event:
ListBox myListBox = myFormView.FindControl("myListBox") As ListBox;
if (myListBox != null) {
myListBox.SelectedIndex++;
}
In the OnClick event handler for the submit button.
You could find the ListBox control from the controls list of the FormView and then increase the selectedIndex
something like this:
public void Button1_Click(object sender, EventArgs e)
{
foreach (Control c in fv1.Controls)
{
if (c is ListBox)
{
ListBox lbx = c as ListBox;
++lbx.SelectedIndex;
}
}
}
Well if you want to increase the selectedindex when a button is clicked, what about increasing it in the button click event? Where are you trying to set it that you're having problems?
精彩评论