Dishant
Dishant

Reputation: 41

Selecting gridview row with a radiobutton in ASP.Net

I would like to have a gridview with a radiobutton column in order to select just a row and do some task. I added a templatefield but when I run the application, it let me select all rows, but what I want is select only one. How can I do that? I mean, I want the radiobutton column to work as a group.

Upvotes: 2

Views: 5770

Answers (3)

qJake
qJake

Reputation: 17119

Did you set the "GroupName" property? Something like this, for example:

<asp:GridView runat="server" ID="MyGridView">
    <Columns>
        <asp:TemplateField HeaderText="Select">
            <ItemTemplate>
                <asp:RadioButton runat="server" ID="myRB" GroupName="MyRadioButton" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Some Other Column">
            <ItemTemplate>
                <%-- other stuff here --%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

You could also replace the <asp:RadioButton> with a standard <Input> field, and then bind the Value="" property to something unique (like a Row ID, for example).

Upvotes: 0

DavRob60
DavRob60

Reputation: 3557

Microsoft made a Tutorial for that

Upvotes: 2

Edwin de Koning
Edwin de Koning

Reputation: 14387

Try to instantiate the radiobutton markup as a literal inside your template, for example like this:

  internal class RadioButtonTemplate : ITemplate, INamingContainer
        {
            public void InstantiateIn(Control container)
            {
                Literal radButton = new Literal();
                radButton.ID = "RadioButtonMarkup";
                radButton.Text = "<input type='radio' name='myRadioButton' id='myRadioButton' value='<%# Eval(" + "CategoryID" + "%>'/>";
                container.Controls.Add(radButton);
            }
        }

Upvotes: 0

Related Questions