下面結(jié)合js實(shí)現(xiàn)單選.
呈現(xiàn)頁:
<asp:GridView ID="GridView1" Width="960px" EmptyDataText="暫無標(biāo)書可操作" DataKeyNames="ID" AutoGenerateColumns="false" runat="server" onrowdatabound="GridView1_RowDataBound" > <Columns> <asp:TemplateField HeaderText="選擇"> <ItemTemplate> <asp:RadioButton ID="RadioButton1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="ID" HeaderText="標(biāo)書編號" />
</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函數(shù) } }
}
需要添加的javascript函數(shù):
<script type="text/javascript"> var last=null; function single(obj) { if(last==null) //第一次選擇RadioButton時賦id值給last { last=obj.id; } else //第一次以后的每一次都在這運(yùn)行,把上此的RadioButton.Checked=false,記下此次的obj.name { var lo=document.getElementById(last); lo.checked=false; last=obj.id; } obj.checked="checked"; //添加checked屬性,以便在上邊賦值為false } </script>
|