2012-01-19 71 views
-1

在我的.NET Web窗体页面中,我有一个ListBox和一些允许我对项目进行排序的jQuery。我选择了几个项目,然后遍历它们以保存到我的数据库。我想保存项目排序的顺序,但是,我无法使其正常工作。我隐藏显示:无法按排序顺序保存列表框项目

protected void session_DetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) 
    { 
     panel_SqlDataSource.Delete(); 
     ListBox panel_ListBox = session_DetailsView.FindControl("panel_ListBox") as ListBox; 
     int so = 0; 
     for (int i=0; i < panel_ListBox.Items.Count; i++) 
     { 
      if (panel_ListBox.Items[i].Selected == true) 
      { 
       so++; 
       panel_SqlDataSource.InsertParameters["presenterID"].DefaultValue = panel_ListBox.Items[i].Value; 
       panel_SqlDataSource.InsertParameters["sortOrder"].DefaultValue = so.ToString(); 
       panel_SqlDataSource.Insert(); 
      } 
     } 
    } 
+0

忘记的时刻的数据库,应只需跟踪输出以确定您是否期望发生的事情正在发生。然后担心数据库。 – demoncodemonkey

+1

也粘贴你的jQuery代码... – demoncodemonkey

回答

0

呀所以您的订单被客户端上的变化可能不会影响该命令,它是在服务器上为很可能会在某个地方的视图状态定义。您的控件会在最初显示时返回。你需要以某种方式跟踪事物的顺序 - 将其从表单变量或其他任何东西中拉出来。

0

ASP .Net不知道ListBox中的项目在客户端已更改的事实。可能您正在使用jQuery.sortable方法对列表框项进行排序。您需要使用“serialize”选项调用sortable并将该值保存到隐藏字段中。当您发布表单时,您会读取该隐藏字段的值,并将新订单保存到数据库中。

因此,对于样品,如果有ID为1,2的对象,3有新的订单2,3,1然后创建更新/插入方法是这样的:

update tbl set sortOrder = 1 where presenterId = 2 
update tbl set sortOrder = 2 where presenterId = 3 
update tbl set sortOrder = 3 where presenterId = 1