Click here to Skip to main content
15,877,384 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a listbox bound to a sqldatasource. I have 2 fields in the sqldatasource, title and display_order.
When page is loaded the listbox is loaded with the title field in order of display_order (select title from table order by display_order).
I have 2 button controls(up and down) alongside the listbox so that user can move the selected items up and down.
When the user rearrange the listitems order, I want to call an sqldatasource update() and populate the display_order with the index values of the listbox. How should I do this? Please help.

Thanks
Sameera

[Moved from OP's answer]
no i dont have listbox index no's in my database field. the display_order field is populated with random numbers. I will copy my code below.

VB
Imports System.Web.UI.WebControls
Partial Class Default2
    Inherits System.Web.UI.Page

        Protected Sub down_Click(sender As Object, e As System.EventArgs) Handles down.Click
        'Make sure our item is not the last one on the list.
        If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
            'Insert places items above the index you supply, since we want
            'to move it down the list we have to do + 2
            Dim I = ListBox1.SelectedIndex + 2
            ListBox1.Items.Insert(I, ListBox1.SelectedItem)
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
            ListBox1.SelectedIndex = I - 1
        End If


    End Sub

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        For Each ListItem In ListBox1.Items
'this is not working....
            sdssiteorder.UpdateParameters("SITE_MAP_DISPLAY_ORDER").DefaultValue = ListBox1.Items.IndexOf(ListItem)

            sdssiteorder.Update()
        Next

    End Sub
End Class

ASP.NET
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Button ID="up" runat="server" Text="up" />
                <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" 
                    DataSourceID="sdssiteorder" DataTextField="PAGE" 
                    DataValueField="SITE_MAP_DISPLAY_ORDER">
                </asp:ListBox>
                <asp:Button ID="down" runat="server" Text="down" />
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:SqlDataSource ID="sdssiteorder" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
            SelectCommand="SELECT PAGE_ID, TITLE || ' : ' || SUB_TITLE AS PAGE, SITE_MAP_DISPLAY_ORDER FROM ISD_PAGES WHERE (SITE_SECTION_ID = 2) AND (SITE_SUBSECTION_ID = 1) ORDER BY SITE_MAP_DISPLAY_ORDER" 
            UpdateCommand="UPDATE "ISD_PAGES" SET   "SITE_MAP_DISPLAY_ORDER" = :SITE_MAP_DISPLAY_ORDER where site_section_id = 2 and site_subsection_id =1">
            <UpdateParameters>
                <asp:Parameter Name="SITE_MAP_DISPLAY_ORDER" />
            </UpdateParameters>
        </asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>
Posted
Updated 15-Dec-11 5:34am
v7

1 solution

Hi,

what you need to do is , you need to update every item of the list. so on click event of your up/down arrow key execute Update method. Update method will update list item order for every item.

hope this will help you,

thanks
-amit.
 
Share this answer
 
Comments
smilingsameera 15-Dec-11 6:35am    
Thanks amit.
Im pretty new in this field. so sorry for my ignorance.
how should I connect the listbox index to the the display_order field in my sqldatasource. Right now the listbox is bound to the sqldatasource. In the up/down button click event I can add a for-loop for every list item and call sqldatasource.update(). then how should I pass the listbox index as the update parameter? can u give me an example code please?
Thanks
Sameera
AmitGajjar 15-Dec-11 7:31am    
hi,

you do not need to do foreach for every item, what you need to do is just call Update method once user click up/down button, and in Update method you need to update every list items.

hope this will help you.

thanks
-amit.
smilingsameera 15-Dec-11 7:54am    
Hi amit, I think I wasnt clear enough. i want the index numbers to be added to the display_order field instead of the original values in there.
AmitGajjar 15-Dec-11 7:57am    
Hi,

you do have index number in your database right, then at the time of Update method call create one array. add every item in it and use array index and it's value. this may solve your problem. you need to pass this array in your Storedprocedure to update whole table data.
AmitGajjar 16-Dec-11 1:39am    
Hi,

you need index in database and sort your data through that index in your list , that way you can change order as well as display it in your listbox.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900