ListBox 上移下移
时间:2010-09-30 来源:凭栏处
前台HTML :
代码
<div style="width: 600px; margin: 0px auto;">
<div style="float: left">
<asp:ListBox ID="lsBoxHotWord" runat="server" Height="300px"></asp:ListBox>
</div>
<div style="float: left; margin-top: 40px;">
<asp:Button ID="btnTop" runat="server" Text="上移" Width="150px" Height="50px" OnClick="btnTop_Click" />
<br />
<asp:Button ID="btnBottom" runat="server" Text="下移" Width="150px" Height="50px" OnClick="btnBottom_Click" />
<br />
<span style="font-size: 12px; color: Red">排序规则:
<br />
     -按排序序号和添加时间排序
<br />
     -序号越小越靠前,最新添加的最前显示 </span>
</div>
</div>
{
if (lsBoxHotWord.SelectedIndex > 0)
{
string name = lsBoxHotWord.SelectedItem.Text;
string ID = lsBoxHotWord.SelectedItem.Value;
int index = lsBoxHotWord.SelectedIndex;
lsBoxHotWord.SelectedItem.Text = lsBoxHotWord.Items[index - 1].Text;
lsBoxHotWord.SelectedItem.Value = lsBoxHotWord.Items[index - 1].Value;
lsBoxHotWord.Items[index - 1].Text = name;
lsBoxHotWord.Items[index - 1].Value = ID;
lsBoxHotWord.SelectedIndex--;
}
int t = lsBoxHotWord.Items.Count;
for (int i = 0; i < t; i++)
{
bool isSuess = HotWordMgr.UpdateWordSort(i.ToString(), lsBoxHotWord.Items[i].Value);
}
DataBingLv();
}
protected void btnBottom_Click(object sender, EventArgs e)
{
if (lsBoxHotWord.SelectedIndex >= 0 && lsBoxHotWord.SelectedIndex < lsBoxHotWord.Items.Count - 1)
{
string name = lsBoxHotWord.SelectedItem.Text;
string ID = lsBoxHotWord.SelectedItem.Value;
int index = lsBoxHotWord.SelectedIndex;
lsBoxHotWord.SelectedItem.Text = lsBoxHotWord.Items[index + 1].Text;
lsBoxHotWord.SelectedItem.Value = lsBoxHotWord.Items[index + 1].Value;
lsBoxHotWord.Items[index + 1].Text = name;
lsBoxHotWord.Items[index + 1].Value = ID;
lsBoxHotWord.SelectedIndex++;
}
int t = lsBoxHotWord.Items.Count;
for (int i = 0; i < t; i++)
{
bool isSuess = HotWordMgr.UpdateWordSort(i.ToString(), lsBoxHotWord.Items[i].Value);
}
DataBingLv();//重新绑定数据方法
}
<div style="float: left">
<asp:ListBox ID="lsBoxHotWord" runat="server" Height="300px"></asp:ListBox>
</div>
<div style="float: left; margin-top: 40px;">
<asp:Button ID="btnTop" runat="server" Text="上移" Width="150px" Height="50px" OnClick="btnTop_Click" />
<br />
<asp:Button ID="btnBottom" runat="server" Text="下移" Width="150px" Height="50px" OnClick="btnBottom_Click" />
<br />
<span style="font-size: 12px; color: Red">排序规则:
<br />
     -按排序序号和添加时间排序
<br />
     -序号越小越靠前,最新添加的最前显示 </span>
</div>
</div>
后台代码:
代码 protected void btnTop_Click(object sender, EventArgs e){
if (lsBoxHotWord.SelectedIndex > 0)
{
string name = lsBoxHotWord.SelectedItem.Text;
string ID = lsBoxHotWord.SelectedItem.Value;
int index = lsBoxHotWord.SelectedIndex;
lsBoxHotWord.SelectedItem.Text = lsBoxHotWord.Items[index - 1].Text;
lsBoxHotWord.SelectedItem.Value = lsBoxHotWord.Items[index - 1].Value;
lsBoxHotWord.Items[index - 1].Text = name;
lsBoxHotWord.Items[index - 1].Value = ID;
lsBoxHotWord.SelectedIndex--;
}
int t = lsBoxHotWord.Items.Count;
for (int i = 0; i < t; i++)
{
bool isSuess = HotWordMgr.UpdateWordSort(i.ToString(), lsBoxHotWord.Items[i].Value);
}
DataBingLv();
}
protected void btnBottom_Click(object sender, EventArgs e)
{
if (lsBoxHotWord.SelectedIndex >= 0 && lsBoxHotWord.SelectedIndex < lsBoxHotWord.Items.Count - 1)
{
string name = lsBoxHotWord.SelectedItem.Text;
string ID = lsBoxHotWord.SelectedItem.Value;
int index = lsBoxHotWord.SelectedIndex;
lsBoxHotWord.SelectedItem.Text = lsBoxHotWord.Items[index + 1].Text;
lsBoxHotWord.SelectedItem.Value = lsBoxHotWord.Items[index + 1].Value;
lsBoxHotWord.Items[index + 1].Text = name;
lsBoxHotWord.Items[index + 1].Value = ID;
lsBoxHotWord.SelectedIndex++;
}
int t = lsBoxHotWord.Items.Count;
for (int i = 0; i < t; i++)
{
bool isSuess = HotWordMgr.UpdateWordSort(i.ToString(), lsBoxHotWord.Items[i].Value);
}
DataBingLv();//重新绑定数据方法
}
相关阅读 更多 +