0

I have a FormView that contains three radio buttons which have values of 1, 2, and 3 respectively. I need to bind the radio buttons to a database field in the FormView's datasource (I do not want to use a RadioList becuase I want to control the formatting, plus I want to know how to do this generally).

I figured out how to bind the data in the code behind to set the field values in the database. Now, I'd like to read the field value from the database to select which radio button is checked. I'm able to do this by running code during the page load that reads it from the database. However, is it possible to do this from the datas ource load event instead?

Html and FormView code:

<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="MyRecordID" DefaultMode="Edit" OnItemUpdating="FormView1_ItemUpdating" >
    <EditItemTemplate>
        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />

        <label>Options:</label>
        <asp:RadioButton ID="RadioButton1" runat="server" GroupName="Test1" Text='1' />
        <asp:RadioButton ID="RadioButton2" runat="server" GroupName="Test1" Text='2' />
        <asp:RadioButton ID="RadioButton3" runat="server" GroupName="Test1" Text='3' />
    </EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource 
    ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="MyConnString" 
    SelectCommand="SELECT * FROM MYTABLE WHERE MYRECORDID=1"
    UpdateCommand="UPDATE MYTABLE SET [MYRECORDOPTION]=@MyRecordOption WHERE MYRECORDID=1" >
    <UpdateParameters>
        <asp:parameter Type="Int32" Name="MyRecordOption" /> 
    </UpdateParameters>
</asp:SqlDataSource>

Code behind:

Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
'I could do something like this, but would prefer avoiding a database hit.
   
    Dim vRadioButton01 As RadioButton = FormView1.FindControl("RadioButton01")
    Dim vRadioButton02 As RadioButton = FormView1.FindControl("RadioButton02")
    Dim vRadioButton03 As RadioButton = FormView1.FindControl("RadioButton03")

    cn = New SqlConnection(db)
    cn.Open()

    sq = "SELECT * FROM [MYTABLE] WHERE MYRECORDID = 1"
    cm = New SqlCommand(sq, cn)
    dr = cm.ExecuteReader()

    If dr.Read() Then
        If dr("MyRecordOption") = 1 Then
            vRadioButton01.Checked = true
        ElseIf dr("MyRecordOption") = 2 Then
            vRadioButton02.Checked = true
        ElseIf dr("MyRecordOption") = 3 Then
            vRadioButton03.Checked = true
        End If
    End If

    dr.Close()
    cn.Close()
    dr = Nothing
    cm = Nothing
    cn = Nothing
End Sub

Protected Sub FormView1_ItemUpdating(ByVal Sender As Object, ByVal e As FormViewUpdateEventArgs) Handles FormView1.ItemUpdating
'Because I could not bind the radio buttons in the FormView, I'm manually binding them.

    Dim vRadioButton01 As RadioButton = FormView1.FindControl("RadioButton01")
    Dim vRadioButton02 As RadioButton = FormView1.FindControl("RadioButton02")
    Dim vRadioButton03 As RadioButton = FormView1.FindControl("RadioButton03")

    If vRadioButton01.Checked Then
        e.NewValues("MyRecordOption") = 1
    ElseIf vRadioButton02.Checked Then
        e.NewValues("MyRecordOption") = 2
    ElseIf vRadioButton03.Checked Then
        e.NewValues("MyRecordOption") = 3
    End If
End Sub


Private Sub SqlDataSource1_Load(sender As Object, e As EventArgs) Handles SqlDataSource1.Load
'Rather than reading from database. I'd like to utilize the Datasource since it's already reading it.
'Something similar to the below that works (this doesn't).

    'If e.value("MyRecordOption") = 1 Then
    '   vRadioButton01.Checked = true
    'ElseIf e.value("MyRecordOption") = 2 Then
    '   vRadioButton02.Checked = true
    'ElseIf e.value("MyRecordOption") = 3 Then
    '   vRadioButton03.Checked = true
    'End If
End Sub
3
  • You're going to have to bind the datasource again, or save the datasource to a session or viewstate variable then read through it whenever you need to Commented Apr 12, 2023 at 23:33
  • use a Forward-Only cursor with lock type set to Read-Only. This type of cursor is also known as a "firehose cursor." Commented Apr 13, 2023 at 14:00
  • Thank you for alternate suggestions, but I want to know how to specially read data from SqlDataSource object. Commented Apr 22, 2023 at 20:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.