- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- CheckBox chkbox = (CheckBox)e.Row.FindControl("chkSelect");
- string script = "function HighlightSelected(selectedCheckbox, RowState){" +
- "if (selectedCheckbox.checked) selectedCheckbox.parentElement.parentElement.style.backgroundColor='#D6E3F7';" +
- "else { if (RowState == '0' ) selectedCheckbox.parentElement.parentElement.style.backgroundColor = '#FFFFFF'; " +
- "else selectedCheckbox.parentElement.parentElement.style.backgroundColor='#FFFFFF';}}";
- Page.ClientScript.RegisterStartupScript(this.GetType(), "RowDataBoundScript", script, true);
- chkbox.Attributes.Add("onclick", "HighlightSelected(this, '" + Convert.ToString(e.Row.RowState) + "');");
- }
- }
前台代码
- protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.DataRow)
- {
- CheckBox chkbox = (CheckBox)e.Row.FindControl("chkSelect");
- string script = "function HighlightSelected(selectedCheckbox, RowState){" +
- "if (selectedCheckbox.checked) selectedCheckbox.parentElement.parentElement.style.backgroundColor='#D6E3F7';" +
- "else { if (RowState == '0' ) selectedCheckbox.parentElement.parentElement.style.backgroundColor = '#FFFFFF'; " +
- "else selectedCheckbox.parentElement.parentElement.style.backgroundColor='#FFFFFF';}}";
- Page.ClientScript.RegisterStartupScript(this.GetType(), "RowDataBoundScript", script, true);
- chkbox.Attributes.Add("onclick", "HighlightSelected(this, '" + Convert.ToString(e.Row.RowState) + "');");
- }
- }
- <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" AutoGenerateColumns="False" DataKeyNames="stoneID" OnRowDataBound="GridView1_RowDataBound">
- <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
- <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
- <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
- <Columns>
- <asp:TemplateField>
- <ItemTemplate>
- <asp:CheckBox ID="chkSelect" runat="server" />
- <asp:Label ID="stoneID" runat="server" Text='<%# Eval("stoneID") %>' Visible="false" />
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>