用js操作gridview的RadioButton值
时间:2011-05-19 来源:fc5110
前台:
<asp:GridView ID="gridViewListNew" runat="server" AutoGenerateColumns="False" AllowSorting="True"
OnSorting="GridViewListSorting" DataKeyNames="FamiliesID" Visible="false" OnRowDataBound="GridViewNew_RowDataBound"
ClientIDMode="Static" CssClass="jqGrid">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:HiddenField ID="hdfCountieID" runat="server" Value='<%#Eval("CountieID") %>' />
<asp:HiddenField ID="hdfTownShipID" runat="server" Value='<%#Eval("TownShipID") %>' />
<asp:HiddenField ID="hdfVillageID" runat="server" Value='<%#Eval("VillageID") %>' />
<asp:HiddenField ID="hdfGroupID" runat="server" Value='<%#Eval("GroupID") %>' />
<asp:HiddenField ID="primayKeyId" Value='<%#Eval("FamiliesID")%>' runat="server" />
<asp:HiddenField ID="hidLockStatus" Value='<%#Eval("FamilyLockStatus")%>' runat="server" />
<asp:RadioButton ID="rbFamily" runat="server" GroupName="choose"/>
</ItemTemplate>
<HeaderStyle CssClass="alignCenter" Width="50px" />
</asp:TemplateField>
<Columns>
</asp:GridView>
<asp:HiddenField ID="hfrbFamilyid" runat="server"/>
js
<script>
function onClientClick(selectedId, memberid) {
//用隐藏控件记录下选中的行号
var inputs = document.getElementById("<%=gridViewListNew.ClientID%>").getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "radio") {
if (inputs[i].id == selectedId) {
inputs[i].checked = true;
document.getElementById("<%=hfrbFamilyid.ClientID %>").value = memberid;
}
else
inputs[i].checked = false;
}
}
}
</script>
gridview绑定事件中:
private void GridViewNewRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
InitControl.ShowSortedIcon(gridViewListNew, e.Row, ViewState["SortExpression"].ToString(), ViewState["SortDirection"].ToString());
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hddf = e.Row.Cells[0].FindControl("primayKeyId") as HiddenField; //表主键ID
RadioButton rb = (RadioButton)e.Row.FindControl("rbFamily");
if (rb != null)
rb.Attributes.Add("onclick",
"onClientClick('" + rb.ClientID + "','" + hddf.Value +
"')");
}
}
后台获取直接用 hfrbFamilyid.value就可以了