I would like to have a gridview with a radiobutton column in orde开发者_开发知识库r 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.
Microsoft made a Tutorial for that
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);
}
}
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).
精彩评论