1

I have one ListBox which has three Items in it. And also has one GridView and One Button.

I want to move the selected items in the listbox to the GridView when the Button is clicked.

Now the problem is, if I selects the third item in the listbox and clicks the button, It always moves the first item in the ListBox to the GridView.

How to solve this?

    <asp:ListBox ID="lbDrawing" runat="server" AutoPostBack="false" Height="260px"  TabIndex="12" Width="150px"></asp:ListBox>

In the Button click event, I select the item by the following C# code,

string itemsname = lbDrawing.Items[lbDrawing.SelectedIndex].Text;

Anyone please help me.

Here, I fill the ListBox based the DropDownList SelectedIndex Changed Event. So I can not bind the ListBox in the Page is not PostBack Block.

2
  • Have you tried For each loop ? Commented Mar 19, 2011 at 6:22
  • @Mitul: No, I havent tried. On which basis I have to do loop? Commented Mar 19, 2011 at 6:27

2 Answers 2

2

Looks like that the listbox is being bound even in the postback. Check and make sure that the DataBind of the listbox is within the if (!IsPostBack) {} block.

Sign up to request clarification or add additional context in comments.

1 Comment

actually I am filling the listbox in the DropDownList selectedindex changed event. So i can not place it inside the Not Post Back block.
0

Check this out.

protected void Button1_Click(object sender, EventArgs e)
    {


        foreach (ListItem lst in ListBox1.Items)
        {
            if (lst.Selected)
            {
                //TODO: Apply checked logic here.
            }

        }

    }

1 Comment

You could use the SelectedItems property of the ListBox. Using the collection will eliminate the requirement for an if-block.

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.