从datagridview保存dropdownlist selectedvalue

时间:2015-07-23 22:36:21

标签: vb.net

我有问题的数据网格视图,然后是每个问题的可能答案的下拉列表。我想在点击btnSaveQuestions_Click事件时保存AnswerID(dropdownlist.selecteditem.value)。但是,在尝试之后,我的Visual Web Developer 2010版本没有给我任何错误,但是在Internet Explorer中查看(版本10.0.9200.17377,是公司策略要求我在Internet Explorer中可以使用它)时,它指出line" Dim AnswerID as String = ddAnswer.SelectedValue"未设置为对象的实例。任何帮助将不胜感激。



<asp:DataGrid id="dgFacilityQuestions_DataGrid" runat="server" cssClass="dgBlue" horizontalalign="center" width="90%" OnItemCreated="dgFacilityQuestions_DataGrid_ItemCreated" OnItemDataBound="dgFacilityQuestions_DataGrid_ItemDataBound" CellPadding="3"
AutoGenerateColumns="False">
  <HeaderStyle backcolor="#A09CBF" />
  <Columns>
    <asp:BoundColumn DataField="QuestionID" ReadOnly="True" HeaderText="QuestionId" Visible="False"></asp:BoundColumn>
    <asp:BoundColumn DataField="QuestionText" HeaderText="Question Text">
      <ItemStyle width="60%" horizontalalign="left" />
    </asp:BoundColumn>
    <asp:TemplateColumn HeaderText="Answers">
      <ItemTemplate>
        <asp:DropdownList id="ddAnswers" runat="server" DataValueField="AnswerID" DataTextField="AnswerText"></asp:DropdownList>
      </ItemTemplate>
    </asp:TemplateColumn>
  </Columns>
</asp:DataGrid>
&#13;
&#13;
&#13;

&#13;
&#13;
Sub dgFacilityQuestionsDataGrid()
        ' create the SQL query that will populate the grid, then call, then bind
        Dim SQLString1 As String = "SELECT QuestionID, QuestionText FROM Questions"
        dgFacilityQuestions_DataGrid.DataSource = Global.ASP.Global.SQLSelect(SQLString1, "Proposal")
        dgFacilityQuestions_DataGrid.DataBind()
    End Sub ' End dgFacilityQuestionsDataGrid()
    
    Sub dgFacilityQuestions_DataGrid_ItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
        ' datagrid formatting before data is bound to the datagrid - this does not care what data is put into the datagrid
        Select Case e.Item.ItemType
            Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem
                ' add additional attributes based on mouseover and mouse out user actions
                e.Item.Attributes.Add("onMouseOver", "this.style.backgroundColor = '#A09CBF'")
                e.Item.Attributes.Add("onMouseOut", "this.style.backgroundColor = '#FFFFFF'")
                ' add buttons as necessary for control if buttons are within the datagrid
                ' Cells(#) and Controls(0) correspond to what column that button resides in the table starting at zero (0)
                
        End Select
    End Sub ' End dgFacilityQuesitonsDataGrid_ItemCreated()
    
    Sub dgFacilityQuestions_DataGrid_ItemDataBound(ByVal Sender As Object, ByVal e As DataGridItemEventArgs)
        ' datagrid formatting once data is bound - this depends on what data is actually produced for the datagrid
        If e.Item.ItemType = ListItemType.Item Or _
        e.Item.ItemType = ListItemType.AlternatingItem Then
            If lblQuestionsDone.Text = "False" Then
                Dim mydropdownlist As DropDownList = e.Item.FindControl("ddAnswers")
                Dim SQLString As String = "SELECT AnswerID, AnswerText FROM QuestionAnswersLink WHERE QuestionID = " & e.Item.Cells(0).Text
                Global.ASP.Global.refresh_ddProposal(0, SQLString, mydropdownlist)
            Else
                Dim mydropdownlist As DropDownList = e.Item.FindControl("ddAnswers")
                Dim SQLString As String = "SELECT AnswerID, AnswerText FROM QuestionAnswersLink WHERE QuestionID = " & e.Item.Cells(0).Text
                Global.ASP.Global.refresh_ddProposal(0, SQLString, mydropdownlist)
                ' set the showing value as the one chosen from the database
            End If
            
        End If
    End Sub ' End dgFacilityQuestionsDataGrid_ItemDataBound()
&#13;
&#13;
&#13;

&#13;
&#13;
Sub btnSaveQuestions_Click(sender As Object, e As EventArgs)
        If btnSaveQuestions.Text = "Save Answers" Then ' questions answered for the first time
            Dim i As Integer = 0
            For Each dr As DataGridItem In dgFacilityQuestions_DataGrid.Items
                Dim ddAnswer As DropDownList = CType(dgFacilityQuestions_DataGrid.FindControl("ddAnswers"), DropDownList)
                Dim AnswerID As String = ddAnswer.SelectedValue
                If AnswerID <> 0 Then
                    Global.ASP.Global.SQLInsert("INSERT INTO ProposalFacilityQuestionLink (ProposalFacilityID, AnswerID) VALUES ('" & lblFacilityID.Text & "','" & AnswerID & "')")
                End If
                i = i + 1
            Next
        ElseIf btnSaveQuestions.Text = "Save Changes" Then ' answers changed
            
            
        End If
    End Sub ' End btnSaveQuestions_Click()
&#13;
&#13;
&#13;

0 个答案:

没有答案