后面的代码是在回发后没有找到单选按钮

时间:2013-03-07 10:42:34

标签: asp.net radio-button postback viewstate

我有一个调查页面,我在表单加载事件中以dinamically方式插入单选按钮。当我单击按钮发送结果时,在回发后,单选按钮仍然存在,并且检查状态正确。但是如果我做了一些代码,在后面的代码中,它试图检查其中一个是否被检查(通过FindControl(id)),页面崩溃了。我不能做调试,所以我不知道是什么错误。


<%@ Control language="vb" AutoEventWireup="false" Explicit="True" Inherits="DotNetNuke.UI.Skins.Skin" %>
<%@ Register TagPrefix="dnn" TagName="USER" Src="~/Admin/Skins/User.ascx" %>
<style>
body {background-image:none; background-color:transparent;}
</style>
<div style="float:left; width:420px; height:335px;" id="divPerguntas" enableviewstate="true" runat="server"></div>
<div style="float:left; width:420px; height:75px;"><asp:ImageButton ImageUrl="/Portals/_default/Skins/AdWin/img/Enviar.png" id="imgEnviar" runat="server" style="cursor:pointer;" /></div>

<div id="ContentPane" runat="server"></div>

<script runat="server">
    Protected Sub EnviarClick() Handles imgEnviar.Click

        Dim conexao As SqlConnection
        Dim comando As SqlCommand
        Dim myReader As SqlDataReader
        Dim sql As String

        sql = "SELECT S.SurveyID, SO.SurveyOptionID FROM AdWin_Demo.dbo.Surveys S INNER JOIN AdWin_Demo.dbo.SurveyOptions SO ON S.SurveyID = SO.SurveyID WHERE S.VideoId = " + Request.QueryString("id") + " ORDER BY S.ViewOrder, SO.ViewOrder"
        conexao = New SqlConnection("#CONNECTIONSTRING#")
        conexao.Open()
        comando = New SqlCommand(sql, conexao)

        myReader = comando.ExecuteReader

        If myReader.HasRows Then
            Dim dt As New DataTable
            dt.Load(myReader)
            Dim SurveyID As Integer = 0
            Dim FirstQuestion As Boolean = True
            Dim checkedGroup As Boolean = False
            Dim insertList As New DataTable
            Dim ColumnIDSurveyOption As New DataColumn("c", GetType(Integer))
            insertList.Columns.Add(ColumnIDSurveyOption)
            Dim row As DataRow


            For Each dr As DataRow In dt.Rows
                If FirstQuestion Then
                    SurveyID = dr("SurveyID")
                    FirstQuestion = False
                End If
                If SurveyID <> dr("SurveyID") Then
                    SurveyID = dr("SurveyID")
                    checkedGroup = False
                End If
                If SurveyID = dr("SurveyID") And checkedGroup = False Then
                        Dim radiobuttonToCheck As RadioButton = FindControl(dr("SurveyOptionID").ToString())
                        If radiobuttonToCheck.Checked Then
                        checkedGroup = True
                        row = insertList.NewRow()
                        row("insertList") = dr("SurveyOptionID")
                        insertList.Rows.Add(row)
                        End If
                End If
            Next

            If checkedGroup = True Then
                Dim dnnUserCtrl As New DotNetNuke.Entities.Users.UserController
                For Each dr As DataRow In insertList.Rows
                    sql = "INSERT INTO AdWin_Demo.dbo.Adwin_RespostasDadas (IDUser, SurveyOptionID) SELECT " & dnnUserCtrl.GetCurrentUserInfo.UserID & ", " & dr("SurveyOptionID").ToString()
                    comando = New SqlCommand(sql, conexao)
                    myReader = comando.ExecuteReader
                Next
            Else
                Response.Write("Nao respondeste a tudo")
            End If
        End If

        myReader.Close()
        conexao.Close()


    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Me.EnableViewState = True

        'If Not Page.IsPostBack Then


        Dim conexao As SqlConnection
        Dim comando As SqlCommand
        Dim myReader As SqlDataReader
        Dim sql As String

        sql = "SELECT S.SurveyID, S.Question, SO.OptionName, SO.SurveyOptionID FROM AdWin_Demo.dbo.Surveys S INNER JOIN AdWin_Demo.dbo.SurveyOptions SO ON S.SurveyID = SO.SurveyID WHERE S.VideoId = " + Request.QueryString("id") + " ORDER BY S.ViewOrder, S.SurveyID, SO.ViewOrder"
        conexao = New SqlConnection("#CONNECTIONSTRING#")
        conexao.Open()
        comando = New SqlCommand(sql, conexao)

        myReader = comando.ExecuteReader


        If myReader.HasRows Then
            Dim dt As New DataTable
            dt.Load(myReader)
            Dim SurveyID As Integer = 0
            Dim FirstQuestion As Boolean = True
            Dim ActualDiv As System.Web.UI.HtmlControls.HtmlGenericControl

            For Each dr As DataRow In dt.Rows
                If SurveyID = dr("SurveyID") Then

                    Dim rb As New RadioButton
                    rb.GroupName = "Group" & SurveyID.ToString()
                    rb.ID = dr("SurveyOptionID").ToString()
                    rb.Text = dr("OptionName").ToString()
                    rb.EnableViewState = True
                    ActualDiv.Controls.Add(rb)
                    ActualDiv.Controls.Add(New LiteralControl("<br/>"))

                Else
                    SurveyID = dr("SurveyID")

                    If FirstQuestion = False Then
                        Dim hr2 As New HtmlGenericControl("hr")
                        divPerguntas.Controls.Add(hr2)
                    End If

                    Dim div As New System.Web.UI.HtmlControls.HtmlGenericControl("DIV")
                    div.Style.Add("float", "left")
                    div.Style.Add("width", "400px")
                    div.Style.Add("color", "#777")
                    div.Style.Add("font-size", "11px")
                    div.Style.Add("line-height", "15px")
                    divPerguntas.Controls.Add(div)
                    ActualDiv = div

                    ActualDiv.InnerText = dr("Question").ToString()
                    ActualDiv.Controls.Add(New LiteralControl("<br/>"))

                    Dim rb As New RadioButton
                    rb.GroupName = "Group" & SurveyID.ToString()
                    rb.ID = dr("SurveyOptionID").ToString()
                    rb.Text = dr("OptionName").ToString()
                    rb.EnableViewState = True
                    ActualDiv.Controls.Add(rb)
                    ActualDiv.Controls.Add(New LiteralControl("<br/>"))

                    FirstQuestion = False
                End If
            Next
            Dim hr As New HtmlGenericControl("hr")
            divPerguntas.Controls.Add(hr)
        End If

        myReader.Close()
        conexao.Close()

    End Sub
</script>

2 个答案:

答案 0 :(得分:0)

尝试在PreInit事件中向页面添加控件,看看是否可以正确访问数据。更多细节http://msdn.microsoft.com/en-us/library/ms178472.aspx

答案 1 :(得分:0)

Dim radiobuttonToCheck As RadioButton = FindControl(dr("SurveyOptionID").ToString())

- &GT;

Dim radiobuttonToCheck As RadioButton = Me.FindControl(dr("SurveyOptionID").ToString())