选择一个带有单选按钮ASP.NET的Gridview行

本文关键字:ASP 单选按钮 NET Gridview 一个 选择 | 更新日期: 2023-09-27 18:08:37

我有一个Gridview绑定到一个数据库表与单选按钮列,我想选择一行与单个单选按钮。需要单选按钮组吗?我有上面的代码与复选框,但我想把它改为单选按钮。我认为单选按钮代码是类似的。谢谢你!我用的是ASP。净c#

<asp:GridView ID="DateGrid" runat="server" CellPadding="4" ForeColor="#333333" 
    GridLines="None" >
    <AlternatingRowStyle BackColor="White" />
   <Columns>
   <asp:TemplateField>
   <ItemTemplate>
 <asp:RadioButton ID="RowSelector" runat="server" GroupName="SelectGroup" />
  </ItemTemplate>
 </asp:TemplateField>
 </Columns> 
</asp:GridView>

背后的代码:

foreach (GridViewRow row in DateGrid.Rows) 
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("DateChBox") as CheckBox);
                if (chkRow.Checked)
                {
                    startdate = row.Cells[2].Text;
                    enddate = row.Cells[3].Text;

选择一个带有单选按钮ASP.NET的Gridview行

这可能对你有帮助

前面的代码

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" Width="100%">
        <Columns>
            <asp:TemplateField HeaderText="Select">
                <ItemTemplate><asp:RadioButton ID="RowSelector" runat="server" onclick="checkRadioBtn(this);" /></ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Start Date">
                <ItemTemplate><asp:Label ID="lblStartDate" runat="server" Text='<%# DateTime.Now.ToString("MM/dd/yyyy") %>'></asp:Label></ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="End Date">
                <ItemTemplate><asp:Label ID="lblEndDate" runat="server" Text='<%# DateTime.Now.ToString("MM/dd/yyyy") %>'></asp:Label></ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

当你选中其他单选按钮时,Javascript会取消选中other

<script type="text/javascript">
    function checkRadioBtn(id) {
        var gv = document.getElementById('<%=gv.ClientID %>');
        for (var i = 1; i < gv.rows.length; i++) {
            var radioBtn = gv.rows[i].cells[0].getElementsByTagName("input");
            // Check if the id not same
            if (radioBtn[0].id != id.id) {
                radioBtn[0].checked = false;
            }
        }
    }
</script>

用于绑定的后台代码…

protected void Page_Load(object sender, EventArgs e)
{
    // Check
    if (!IsPostBack)
    {
        // Varaible
        DataTable dt = new DataTable();
        dt.Columns.Add("A");
        dt.Columns.Add("B");
        for (int i = 0; i < 3; i++)  dt.Rows.Add(i.ToString(), i.ToString()); 
        // Bind
        gv.DataSource = dt;
        gv.DataBind();
    }
}

这可以通过在Radio Button上启用AutoPostBack从后端完成:

 <asp:RadioButton ID="RowSelector" runat="server" GroupName="SelectGroup" Checked="false" AutoPostBack="true" OnCheckedChanged="rd_CheckedChanged" />

并在OnCheckedChanged事件中使用以下代码:

static int rdChecked = -1; //Use this variable to store the last checked row
protected void rd_CheckedChanged(object sender, EventArgs e)
{
   if (rdChecked != -1)
   {
       RadioButton rdSelection = DateGrid.Rows[rdChecked].FindControl("RowSelector") as RadioButton;
       rdSelection.Checked = false;
   }
   for (int i = 0; i < DateGrid.Rows.Count; i++)
   {
       RadioButton rdSelection = DateGrid.Rows[i].FindControl("RowSelector") as RadioButton;
       if (rdSelection.Checked)
       {
           rdChecked = i;
       }
   }
}
<asp:GridView ID="DateGrid" runat="server" DataSourceID="sdsData">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:RadioButton ID="chk" ClientIDMode="Static" runat="server" onclick="$('[id=chk]').prop('checked' , '');$(this).prop('checked' , 'checked');"></asp:RadioButton>
            <asp:HiddenField ID="hdn" runat="server" Value='<%# Eval("Fserial") %>' />
        </ItemTemplate>
        <ItemStyle HorizontalAlign="Center" Width="20px" />
    </asp:TemplateField>
    <asp:BoundField DataField="Fname" HeaderText="Name" SortExpression="Fname" />
</Columns></asp:GridView>