日期:2014-05-19  浏览次数:20467 次

怎样多条选择?
在GridView中用Shift和Ctri键同时选择多行,就象资源管理器中选择文件一样,选中的反色显示。并取出来,有用的字段插入到另一个GridView中。
请高手帮忙!!!


------解决方案--------------------
1 . 页面代码 给GridView添加TemplateField ,在TemplateField 下添加
CheckBoxList ,选他的原因是他有 AutoPostBack回送事件,当选择的时候可以发出回送,从而改变背景颜色,或者其它自定义函数,在次的函数是CheckBoxList1_TextChanged
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default.aspx.cs " Inherits= "_Default " %>
<html " >
<head runat= "server ">
<title> Untitled Page </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:GridView ID= "GridView1 " runat= "server ">
<Columns>
<asp:TemplateField HeaderText= "选择 ">
<ItemTemplate>
<asp:CheckBoxList ID= "CheckBoxList1 " runat= "server " AutoPostBack= "True " OnTextChanged= "CheckBoxList1_TextChanged ">
<asp:ListItem > </asp:ListItem>
</asp:CheckBoxList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Text= "Button " />
</div>
</form>
</body>
</html>
2。cs代码,数据源ArrayList
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ArrayList values = new ArrayList();
values.Add(0);
values.Add(1);
values.Add(2);
values.Add(3);
values.Add(4);
values.Add(5);
values.Add(6);
values.Add(2134);
values.Add(123);
values.Add(123);
this.GridView1.DataSource = values;
this.GridView1.DataBind();
}
}
3 单击事件 找出选择的行
protected void Button1_Click(object sender, EventArgs e)
{
CheckBoxList CheckBoxList1 = new CheckBoxList();
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
CheckBoxList1 = ((CheckBoxList)this.GridView1.Rows[i].FindControl( "CheckBoxList1 "));
if (CheckBoxList1.Items[0].Selected)
{
Response.Write(this.GridView1.Rows[i].Cells[1].Text + " <br /> ");
}
}
}
4 。CheckBoxList1改变的时候触发
protected void CheckBoxList1_TextChanged(object sender, EventArgs e)
{
CheckBoxList CheckBoxList1 = new CheckBoxList();
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
CheckBoxList1 = ((CheckBoxList)this.GridView1.Rows[i].FindControl( "CheckBoxList1 "));
if (CheckBoxList1.Items[0].Selected)
{
this.GridView1.Rows[i].BackColor = System.Drawing.Color.Tomato;
}
else
{
this.GridView1.Rows[i].BackColor = System.Drawing.Color.White;
}