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="" />
哪裏的代碼中找到你已經證明?我非常肯定,這個問題的原因是你在每次回發時都會綁定網格,而不是僅僅「If Not Page.IsPostback」。然後沒有人被選中,並且'SelectNumber'保持它的默認值'0' –
解決了問題 - 謝謝。 – SRock2016