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.
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
If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
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
sdssiteorder.UpdateParameters("SITE_MAP_DISPLAY_ORDER").DefaultValue = ListBox1.Items.IndexOf(ListItem)
sdssiteorder.Update()
Next
End Sub
End Class
<%@ 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>