使用单选按钮ASP.NET选择一个Gridview行

时间:2015-08-18 15:45:27

标签: c# datagridview radio-button

我有一个Gridview绑定到带有单选按钮列的数据库表,我想选择一个带单个单选按钮的行。有必要一个单选按钮组吗?我上面的代码带有复选框,但我想将其更改为单选按钮。我认为单选按钮代码是类似的。谢谢。我正在使用ASP.NET 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;

3 个答案:

答案 0 :(得分:4)

这可能对您有所帮助

前面的代码

<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将取消选中其他按钮

<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();
    }
}

答案 1 :(得分:1)

这可以通过在单选按钮上启用 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;
       }
   }
}

答案 2 :(得分:0)

<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>