GridView中实现单选RadioButton

  GridView中实现单选RadioButton   
GridView中实现单选RadioButton - 空空 - Eval的博客
 

 

注意:若在gridview内对radiobutton用组名groupname同名方法行不同,若加上GroupName="skytest"

查看静态页面源代码便知道:两个name值的组名都不同。

 

下面结合js实现单选.

呈现页:

 <asp:GridView ID="GridView1" Width="960px" EmptyDataText="暂无标书可操作"
            DataKeyNames="ID" runat="server" onrowdatabound="GridView1_RowDataBound"
            >
            <Columns>
                <asp:TemplateField HeaderText="选择">
                    <ItemTemplate>
                        <asp:RadioButton ID="RadioButton1" runat="server"    />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="ID" HeaderText="标书编号" />

          </Columns>
  </asp:GridView>

代码页:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                RadioButton rb = (RadioButton)e.Row.FindControl("RadioButton1");
                if (rb != null)
                {
                    rb.Attributes.Add("onclick", "single(this)");  //single(obj)为js函数
                }
            }

        }

需要添加的javascript函数:

<script type="text/javascript">
    var last=null;
    function single(obj)
    {
      if(last==null)    //第一次选择RadioButton时赋id值给last
      {
         last=obj.id; 
      }
      else            //第一次以后的每一次都在这运行,把上此的RadioButton.Checked=false,记下此次的obj.name
      {
        var lo=document.getElementByIdx_x(last);
        lo.checked=false;
        last=obj.name;
      }
      obj.checked="checked";   //添加checked属性,以便在上边赋值为false
    }
    </script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值