Perform some functions on a listbox when i click in htmlbutton to avoid postback.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Abeeez

    Perform some functions on a listbox when i click in htmlbutton to avoid postback.

    I want to perform some functions on a ListBox when I click an HTML button but I to avoid postback to minimise the server round trips....

    Code:
    protected void Assign_Button_Click(object sender, EventArgs e)
            {
                Button clickedButton = sender as Button;
                if (clickedButton == null)
                {
                    return;
                }
    
                if (clickedButton == Add_Button)
                {
                    if (Availableroles_ListBox.SelectedItem != null)
                    {
                        AssignedRoles_ListBox.Items.Add(Availableroles_ListBox.SelectedItem.ToString());
                        Availableroles_ListBox.Items.Remove(Availableroles_ListBox.SelectedItem.ToString());
                    }
                }
                else if (clickedButton == AddAll_Button)
                {
                    foreach (ListItem vv in Availableroles_ListBox.Items)
                    {
                        AssignedRoles_ListBox.Items.Add(vv.ToString());
                    }
                    Availableroles_ListBox.Items.Clear();
                }
                else if (clickedButton == Remove_Button)
                {
                    if (AssignedRoles_ListBox.SelectedItem != null)
                    {
                        Availableroles_ListBox.Items.Add(AssignedRoles_ListBox.SelectedItem.ToString());
                        AssignedRoles_ListBox.Items.Remove(AssignedRoles_ListBox.SelectedItem.ToString());
                    }
                }
                else if (clickedButton == RemoveAll_Button)
                {
                    foreach (ListItem vv in AssignedRoles_ListBox.Items)
                    {
                        Availableroles_ListBox.Items.Add(vv.ToString());
                    }
                    AssignedRoles_ListBox.Items.Clear();
                }
             
            }
    Last edited by Frinavale; Oct 8 '10, 08:51 PM. Reason: Added information from the title to the question body so that the question makes more sense.
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    try to use javascript functions

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Looking over your code, it is going to be very tricky to do this using JavaScript. There is a way, but it isn't straightforward and requires some tricky stuff so that your page requests don't get caught up in ASP.NET Page Validation errors.

      I recommend that you allow the postbacks to the server. You could consider using an UpdatePanel which makes use of Ajax (asynchronous requests/postbacks) so that the process is less noticeable to the end user.

      -Frinny

      Comment

      Working...