点击GridView任意位置,选中所在行(转)

1.在GridView中加入ridiobutton控件
<asp:GridView ID="GridView1" runat="server"
                         DataSourceID="ObjectDataSource1" SkinID="common" OnRowDataBound="GridView1_RowDataBound">
                         <Columns>
                             <asp:BoundField DataField="depId" HeaderText="部门编号" SortExpression="depId"/>
                             <asp:BoundField DataField="depName" HeaderText="部门名称" SortExpression="depName"/>
                             <asp:BoundField DataField="principal" HeaderText="负责人" SortExpression="principal"/>
                             <asp:BoundField DataField="depMemo" HeaderText="备注" SortExpression="depMemo" />
                             <asp:TemplateField InsertVisible="False">
                                 <ItemTemplate>
                                     <input id='<%#"select_" + DataBinder.Eval(Container, "dataitem.depId")%>' name="select" type="radio" />
                                 </ItemTemplate>
                             </asp:TemplateField>
                         </Columns>
                     </asp:GridView>
2.在GridView的RowDataBound事件里写入变色代码
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
     {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             DataRowView row = (DataRowView)e.Row.DataItem;
             string id = row["depId"].ToString();
             e.Row.Attributes.Add("onclick", "select" + id + "checked=true;");
         }
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             e.Row.Attributes.Add("onmouseover", "if(this!=prev){c=this.style.backgroundColor;this.style.backgroundColor='#D8F3C6'}");//当鼠标停留时更改背景色
             e.Row.Attributes.Add("onmouseout", "if(this!=prev){this.style.backgroundColor=c}");//当鼠标移开时还原背景色
             e.Row.Attributes["style"] = "Cursor:hand";//设置悬浮鼠标指针形状为"小手"

             //DataRowView row = (DataRowView)e.Row.DataItem;
             //string id = row["depId"].ToString();
             //e.Row.Attributes.Add("onclick", "select_" + e.Row.Cells[0].Text + ".checked=true;");

             e.Row.Attributes.Add("onclick", "select_" + e.Row.Cells[0].Text + ".checked=true;selectx(this)");
         }
     }
3.在前台js脚本写控制函数
     <script language="javascript" type="text/javascript" >
     var prev=null;
         function selectx(row)    /**//*改变选中行的颜色还原为选中行的颜色*/
         {
             if(prev!=null)
             {
                 prev.style.backgroundColor='#E4F7D8';
             }
             row.style.backgroundColor='#8EC26F';
             prev=row;
            
         }
     </script>最后,记得在后台用Request.Form["select"]来接收选中的值。另外,想效果好些就把那radiobuttou给隐藏了。

转载于:https://www.cnblogs.com/tiantangwater/archive/2008/12/24/1361580.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值