开发者

Selecting gridview row with a radiobutton in ASP.Net

开发者 https://www.devze.com 2023-02-28 15:19 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消