【问题标题】:How to find Radio button control within Gridview如何在 Gridview 中找到单选按钮控件
【发布时间】:2016-03-01 13:37:21
【问题描述】:

我试图在 gridview 中找到一个单选按钮控件,但是对于我选择的任何单选按钮,我都会不断得到 0 的输出。我希望为每个单选按钮分配 1-4 的值,然后选择任何单选按钮,它将被输出到标签。

VB 代码

 Dim SelectNumber As Integer

 For Each row As GridViewRow In MyGridView.Rows

        Dim radbtn1 As RadioButton = TryCast(row.FindControl("a1"), RadioButton)
        Dim radbtn2 As RadioButton = TryCast(row.FindControl("a2"), RadioButton)
        Dim radbtn3 As RadioButton = TryCast(row.FindControl("a3"), RadioButton)
        Dim radbtn4 As RadioButton = TryCast(row.FindControl("a4"), RadioButton)


        If radbtn1.Checked = True Then
            SelectNumber = 1
        ElseIf radbtn2.Checked = True Then
            SelectNumber = 2
        ElseIf radbtn3.Checked = True Then
            SelectNumber = 3
        ElseIf radbtn4.Checked = True Then
            SelectNumber = 4
        End If


    Next

    lblOutput.Text = SelectNumber

源代码

 <asp:GridView ShowHeader="false" AutoGenerateColumns="false" ID="MyGridView" runat="server" GridLines="None">
        <Columns>
            <asp:TemplateField>

                <ItemTemplate>

                    <asp:Label runat="server" ID="QuestionID" Text='<%# Eval("QuestionID")%>' />
                    <asp:Label runat="server" ID="Question" Text='<%# Eval("Question")%>' /><br />
                    <asp:RadioButton GroupName="gnA" Text='<%# Eval("a1")%>' runat="server" ID="ans1" /><br />
                    <asp:RadioButton GroupName="gnA" Text='<%# Eval("a2")%>' runat="server" ID="ans2" /><br />
                    <asp:RadioButton GroupName="gnA" Text='<%# Eval("a3")%>' runat="server" ID="ans3"  /><br />                  
                    <asp:RadioButton GroupName="gnA" Text='<%# Eval("a4")%>' runat="server" ID="ans4" /><hr />
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>

    </asp:GridView>

<asp:Button ID="btnSubmit" runat="server" Text="Complete" />
   <asp:Label ID="lblOutput" runat="server" Text="" />

【问题讨论】:

  • 您显示的代码在哪里?我很确定这个问题的原因是您在每次回发时都对网格进行了数据绑定,而不仅仅是If Not Page.IsPostback。然后没有人被检查,SelectNumber 保持它的默认值,即0
  • 解决了这个问题 - 谢谢。

标签: asp.net vb.net gridview findcontrol


【解决方案1】:

我很确定这个问题的原因是您在每次回发时都对网格进行了数据绑定,而不仅仅是If Not Page.IsPostback。然后没有人被检查并且SelectNumber保持它的默认值0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        ' DataBind the GridView here or call a method therefore '
    End If
End Sub

【讨论】:

    猜你喜欢
    • 2013-12-17
    • 2013-01-09
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 2014-10-12
    • 1970-01-01
    相关资源
    最近更新 更多