标题:如何取得list控件中DataTextField,DataValueField的值。
只看楼主
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
 问题点数:0 回复次数:2 
如何取得list控件中DataTextField,DataValueField的值。

我有一个表,有三个字段。
NewsKindID NewsKindName Order
1 今日报道 0
2 每周快讯 1
3 国内新闻 2
4 国际新闻 3

如下图。我将list控件中DataTextField,DataValueField属性的值分别设为NewsKindID和NewsKindName。



我现在想通过点击list控件旁边的“向上箭头”和“向下箭头”键来改变list中值的排序。

如,当list的SelectedItem值为第三项“国内新闻”时,单击“向上箭头”,将会使其Order值由原先的"2"变成"1",
同时“每周快讯”项的Order值由"1"变成"2",其“向上箭头”的代码该如何写?

[此贴子已经被作者于2006-2-21 23:49:37编辑过]

搜索更多相关主题的帖子: list 控件 
2006-02-21 21:02
islet
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:89
帖 子:6548
专家分:0
注 册:2005-1-28
得分:0 
string id=newslist.SelectedItem.Value.TOString();
向上:sql="update 表 set Order=Order+1 where NewsKindID="+id;
执行
sql="update 表 set Order=Order-1 where Order=(select Order from 表 where id="+id+") and NewsKindID<>"+id;
执行
向下:sql="update 表 set Order=Order-1 where Order="+newslist.SelectedItem.Value;
执行
sql="update 表 set Order=Order+1 where Order=(select Order from 表 where id="+id+") and NewsKindID<>"+id;
执行

要注意第一个和最后一个的移动限制
2006-02-22 08:50
C_B_Lu
Rank: 1
等 级:新手上路
威 望:1
帖 子:453
专家分:0
注 册:2006-1-10
得分:0 
TO: islet
依你的方法,问题已解决,用SELECT嵌套,的确省去了很多中间过程,谢谢。

我的完整代码如下:

private void upBtn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(NewsKindList.SelectedIndex > 0)
{
int nSelectedIndex = NewsKindList.SelectedIndex;
String id = NewsKindList.SelectedItem.Value.ToString();
SqlConnection myConnection = new SqlConnection(GlobalDB.DataBaseDB.ConnectionString);
String cmdText = "UPDATE NewsKinds SET KindOrder = KindOrder - 1 WHERE NewsKindID = " + id;
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
cmdText = "UPDATE NewsKinds SET KindOrder = KindOrder + 1 WHERE KindOrder = (SELECT KindOrder FROM NewsKinds WHERE NewsKindID = " + id + ") AND NewsKindID <> " + id;
myCommand = new SqlCommand(cmdText,myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
BindNewsKindData();
NewsKindList.SelectedIndex = Math.Max(0,(nSelectedIndex - 1));
}
}
private void downBtn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
if(NewsKindList.SelectedIndex < (NewsKindList.Items.Count - 1))
{
int nSelectedIndex = NewsKindList.SelectedIndex;
String id = NewsKindList.SelectedItem.Value.ToString();
SqlConnection myConnection = new SqlConnection(GlobalDB.DataBaseDB.ConnectionString);
String cmdText = "UPDATE NewsKinds SET KindOrder = KindOrder + 1 Where NewsKindID = " + id;
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
cmdText = "UPDATE NewsKinds SET KindOrder = KindOrder -1 WHERE KindOrder = (SELECT KindOrder FROM NewsKinds WHERE NewsKindID = " + id + ") AND NewsKindID <> " + id;
myCommand = new SqlCommand(cmdText,myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
BindNewsKindData();
NewsKindList.SelectedIndex = Math.Min((NewsKindList.Items.Count - 1),(nSelectedIndex + 1));
}
}

帮助那些真正需要帮助的人,是对帮助你的人最好的回报!
2006-02-22 22:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-46516-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.334437 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved