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;
}
}
}

总结:交互多,还有选择时候的遍例都增加了时间,这也许就是windows程序和web的很大区别吧!

你可能感兴趣的:(html,windows,Web,asp)