2016-03-01 66 views
0

我试图在GridView中找到一个单选按钮控件,但我始终得到0的输出用于我选择的任何单选按钮。我希望每个单选按钮被分配1-4的值,然后选择任何单选按钮,它将被输出到标签。如何查找Gridview中的单选按钮控件

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="" /> 
+1

哪里的代码中找到你已经证明?我非常肯定,这个问题的原因是你在每次回发时都会绑定网格,而不是仅仅“If Not Page.IsPostback”。然后没有人被选中,并且'SelectNumber'保持它的默认值'0' –

+0

解决了问题 - 谢谢。 – SRock2016

回答

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