为什么循环时无法从gridview中读取单选按钮?

时间:2018-12-07 08:50:25

标签: c# asp.net gridview c#-4.0

我有一个gridview,它有一个单选按钮。我在网格外有按钮。我想遍历网格并选择单选按钮的值(如果选中)。

我已经做到了,但是对选中的单选按钮不起作用。

 protected void btnCheckOut_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (GridViewRow row in gvBooks.Rows)
                {
                    if (((RadioButton)row.FindControl("rdSelect")).Checked)
                    {
                        Utilities.Utilities.setPageMessage("checked", Utilities.Utilities.severity.error, Page.Master);
                        return;
                    }
                }}

aspx:

<asp:GridView ID="gvBooks" runat="server" DataKeyNames="BookID"
                    AutoGenerateColumns="false" EmptyDataText="No Data Found">
                    <Columns>
                        <asp:TemplateField HeaderText="Select">
                            <ItemTemplate>
                                 <asp:HiddenField runat="server" ID="hdnBookID" ClientIDMode="AutoID" Value='<%# Eval("BookID") %>' />
                                <asp:RadioButton runat="server" ID="rdSelect"/>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Title" HeaderText="Book Title" />
                        <asp:BoundField DataField="ISBN" HeaderText="ISBN" />
                        <asp:BoundField DataField="PublishYear" HeaderText="Publish Year" />
                        <asp:BoundField DataField="CoverPrice" HeaderText="Cover Price" />
                        <asp:TemplateField HeaderText="Status">
                            <ItemTemplate>
                                <asp:Label ID="lableCheckOutStatus" runat="server" Text='<%# Eval("CheckOutStatusDescription") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>

                    </Columns>
                </asp:GridView>

 <asp:Button ID="btnCheckOut" CssClass="button" ClientIDMode="Static" runat="server" Text="Check Out" OnClick="btnCheckOut_Click" />

0 个答案:

没有答案