如何使用列表框的索引更新sqldatasource中的字段? [英] How to update a field in sqldatasource with the index of listbox?
问题描述
我有一个绑定到sqldatasource的列表框.我在sqldatasource中有2个字段,分别是title和display_order.
加载页面时,列表框将按照display_order的顺序加载标题字段(按display_order从表顺序中选择标题).
我在列表框旁边有2个按钮控件(上下),以便用户可以上下移动选定的项目.
当用户重新排列listitems顺序时,我想调用sqldatasource update()并用列表框的索引值填充display_order.我应该怎么做?请帮忙.
谢谢
Sameera
[从OP的答案移出]
不,我的数据库字段中没有列表框索引号. display_order字段填充了随机数.我将在下面复制我的代码.
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
'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
<%@ 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>
推荐答案
ConnectionStrings:ConnectionString %> " =" <%
ConnectionStrings:ConnectionString %>" ProviderName="<%
ConnectionStrings:ConnectionString.ProviderName %> " span> =" 选择PAGE_ID,标题||':'|| SUB_TITLE作为页面,从ISD_PAGES(SITE_SECTION_ID = 2)和(SITE_SUBSECTION_ID = 1)通过SITE_MAP_DISPLAY_ORDER排序" span> =" 更新" SET " SITE_MAP_DISPLAY_ORDER" = :SITE_MAP_DISPLAY_ORDER 其中 site_section_id = 2 和 site_subsection_id = 1" < UpdateParameters > < asp:Parameter 名称 =" / < /UpdateParameters > < /asp:SqlDataSource > < /div > < /form > < /body > < /html >
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>
您需要做的是,您需要更新列表中的每个项目.因此在您的上/下箭头键的点击事件上,执行更新方法. 更新方法将更新每个商品的列表商品顺序.
希望对您有帮助,
谢谢
-amit.
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.
这篇关于如何使用列表框的索引更新sqldatasource中的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!