0

I have a listbox in asp.net I tried when user click on it's item in runtime can change the text of this item without change the order of the the items . So I try to make Edit button when click on it get the item value as

 protected void btnEditListValue_Click(object sender, EventArgs e)
        {
            string listvalue = lstParameters.Items.IndexOf(lstParameters.SelectedItem).ToString();
            string listText = lstParameters.SelectedItem.ToString();
        }

then I will make textbox fill value from string listText to enable user to edit

what shall I do after that to keep listbox order as old without delete and insert again

please help

5
  • Why do you convert the index of the selected item to string at all? What do you want to do with listvalue? Commented Mar 7, 2016 at 9:17
  • @TimSchmelter Something wrong also , listText gets the value of the first item all the time and listvalue just for test I didn't use it Commented Mar 7, 2016 at 10:19
  • Maybe you are databinding the ListBox on every postback instead of only if(!isPostBack){DataBindYourListBox();}. Then all changes or events are lost. Commented Mar 7, 2016 at 10:21
  • @TimSchmelter in if(!isPostBack) I clear the lstParameters.Items.Clear(); and if I remove it will autopostback and append new data on the old one Commented Mar 7, 2016 at 10:34
  • Where do you append data? You should not append items or databind the ListBox at all on postback. Or have you disabled viewstate? But of course, if you clear the items you won't have a selected item other then the first(which means none at all). Commented Mar 7, 2016 at 10:43

1 Answer 1

1

You can overwrite the ListItem.Text, f.e. when the user changed the text:

protected void TextBox1_TextChanged(object sender, EventArgs e)
{
   if(lstParameters.SelectedIndex >= 0 && !String.IsNullOrWhiteSpace(TextBox1.Text))
   {
       ListItem selectedItem = lstParameters.Items[lstParameters.SelectedIndex];
       selectedItem.Text = TextBox1.Text;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.