日期:2014-05-18 浏览次数:20908 次
<asp:GridView ID="dgck" runat="server" AutoGenerateColumns="False" Width ="2200px" Font-Size="X-Small" DataKeyNames="客户号" AllowSorting="True" > <RowStyle HorizontalAlign="Center" Height="20px" /> <Columns> <asp:TemplateField HeaderText="选中一行"> <EditItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </EditItemTemplate> <ItemTemplate> <asp:CheckBox ID="check" runat="server" OnCheckedChanged="check_CheckedChanged" /> </ItemTemplate> </asp:TemplateField> </Columns> <HeaderStyle HorizontalAlign="Center" Font-Size="X-Small" ForeColor="Black" /> <AlternatingRowStyle Font-Size="X-Small" /> </asp:GridView>
public void dgck_bind()//绑定gridview数据. {..............} protected void check_CheckedChanged(object sender, EventArgs e)//checkbox触发事件 { for (int i = 0; i < dgck.Rows.Count; i++) { CheckBox cb = (CheckBox)dgck.Rows[i].FindControl("check");//找到id="dgck"的gridview的模板中id="check"的checkbox,并转换成CheckBox类型。 DataControlFieldCell dc = (DataControlFieldCell)cb.Parent;//得到checkbox所在的单元格 GridViewRow gr = (GridViewRow)dc.Parent;//得到checkbox所在的行 if (gr.Checked == true)//如果点击checkbox打勾 { //不知道怎么写了 } else //如果取消打勾或者没有打勾 { //不知道怎么写了 } } }
protected void GridViewShow_RowDataBound(object sender, GridViewRowEventArgs e) { CheckBox cbx = (CheckBox)e.Row.FindControl("CheckBox1"); if (cbx != null) { if(ViewState["rowIndex"] == null) { ViewState["rowIndex"] = 0 ; } cbx.CssClass = Convert.ToString(ViewState["rowIndex"]); ViewState["rowIndex"] = Convert.ToInt32(ViewState["rowIndex"]) + 1; } } protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { GridBackColor = this.GridViewShow.BackColor;//取