locked
FormView RadioButtonList value RRS feed

  • Question

  • User1510859543 posted

    I am using the code below to try to set the selected value of a RadioButtonList inside a FormView but it is not working.  The test row I have has a value of "shop" in the data item but the RadioButtonList did not get selected.  Any ideas what is missing?

    //markup in FormView
    
            <asp:RadioButtonList ID="rblCheckGoingTo" runat="server" RepeatDirection="Horizontal" onchange="setChange();">
                <asp:ListItem Value="customer" Text="Customer"></asp:ListItem>
                <asp:ListItem Value="shop" Text="Shop"></asp:ListItem>
                <asp:ListItem Value="shop at the end" Text="Shop at the end"></asp:ListItem>
            </asp:RadioButtonList>
    
    
    
    //in code behind in Databound event
    
                Dim rbl As RadioButtonList = row.FindControl("rblCheckGoingTo")
                If Not IsDBNull(fvJobSetup.DataItem("InitialCheckGoingTo")) Then
                    For Each item As ListItem In rbl.Items
                        If item.Value = fvJobSetup.DataItem("InitialCheckGoingTo") Then
                            rbl.SelectedValue = fvJobSetup.DataItem("InitialCheckGoingTo").ToLower
                        End If
                    Next item
                End If
    

    Monday, May 13, 2019 8:51 PM

Answers

  • User665608656 posted

    Hi dlchase,

    According to your question, you could better to make sure that every value in your radiobuttonlist matches your InitialCheckGoingTo successfully. 

    Secondly, due to the incompleteness of your code, I can't infer what row is in this statement :

    dlchase

    Dim rbl As RadioButtonList = row.FindControl("rblCheckGoingTo")

    I have made a simple case according to your idea. You can refer to it.

    <form id="form1" runat="server">
            <div>
                <asp:FormView ID="FormView1" runat="server" DataKeyNames="RowNumber" DataSourceID="SqlDataSource1" OnDataBound="FormView1_DataBound" AllowPaging="True">
                    <ItemTemplate>
                        RowNumber:
                        <asp:Label ID="RowNumberLabel" runat="server" Text='<%# Eval("RowNumber") %>' />
                        <br />
                        FruitName:
                        <asp:Label ID="FruitNameLabel" runat="server" Text='<%# Bind("FruitName") %>' />
                        <br />
                        UnitPrice:
                        <asp:Label ID="UnitPriceLabel" runat="server" Text='<%# Bind("UnitPrice") %>' />
                        <br />
                        Quantity:
                        <asp:Label ID="QuantityLabel" runat="server" Text='<%# Bind("Quantity") %>' />
                        <br />
                        <asp:RadioButtonList ID="rblCheckGoingTo" runat="server" RepeatDirection="Horizontal" onchange="setChange();">
                            <asp:ListItem Value="Apple" Text="Apple"></asp:ListItem>
                            <asp:ListItem Value="Banana" Text="Banana"></asp:ListItem>
                            <asp:ListItem Value="Grapes" Text="Grapes"></asp:ListItem>
                        </asp:RadioButtonList>
                        <br />
                    </ItemTemplate>
                </asp:FormView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString1 %>" SelectCommand="SELECT * FROM [Fruits]"></asp:SqlDataSource>
            </div>
        </form>

    code behind:

    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        Dim rbl As RadioButtonList = TryCast(FormView1.FindControl("rblCheckGoingTo"), RadioButtonList)
        If Not String.IsNullOrEmpty(drv("FruitName").ToString()) Then
            For Each item As ListItem In rbl.Items
                If item.Value = drv("FruitName").ToString() Then
                    rbl.SelectedValue = drv("FruitName").ToString()
                    Exit For
                End If
            Next
        End If
    End Sub

    The result of my work demo: 

     

    If I misunderstanding your question, please feel free to let me know.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 14, 2019 5:09 AM

All replies

  • User-1716253493 posted

    add a hiddenfield near the rbl, bind hf value to InitialCheckGoingTo field

    Use fv databound event, check fv current mode is correct mode for the rbl, i.e if insert mode the rbl must be in insertitemtemplate

    use fv.findcontrols to get the rbl and hf, use hf.value to set rbl selected item

    Tuesday, May 14, 2019 2:32 AM
  • User665608656 posted

    Hi dlchase,

    According to your question, you could better to make sure that every value in your radiobuttonlist matches your InitialCheckGoingTo successfully. 

    Secondly, due to the incompleteness of your code, I can't infer what row is in this statement :

    dlchase

    Dim rbl As RadioButtonList = row.FindControl("rblCheckGoingTo")

    I have made a simple case according to your idea. You can refer to it.

    <form id="form1" runat="server">
            <div>
                <asp:FormView ID="FormView1" runat="server" DataKeyNames="RowNumber" DataSourceID="SqlDataSource1" OnDataBound="FormView1_DataBound" AllowPaging="True">
                    <ItemTemplate>
                        RowNumber:
                        <asp:Label ID="RowNumberLabel" runat="server" Text='<%# Eval("RowNumber") %>' />
                        <br />
                        FruitName:
                        <asp:Label ID="FruitNameLabel" runat="server" Text='<%# Bind("FruitName") %>' />
                        <br />
                        UnitPrice:
                        <asp:Label ID="UnitPriceLabel" runat="server" Text='<%# Bind("UnitPrice") %>' />
                        <br />
                        Quantity:
                        <asp:Label ID="QuantityLabel" runat="server" Text='<%# Bind("Quantity") %>' />
                        <br />
                        <asp:RadioButtonList ID="rblCheckGoingTo" runat="server" RepeatDirection="Horizontal" onchange="setChange();">
                            <asp:ListItem Value="Apple" Text="Apple"></asp:ListItem>
                            <asp:ListItem Value="Banana" Text="Banana"></asp:ListItem>
                            <asp:ListItem Value="Grapes" Text="Grapes"></asp:ListItem>
                        </asp:RadioButtonList>
                        <br />
                    </ItemTemplate>
                </asp:FormView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString1 %>" SelectCommand="SELECT * FROM [Fruits]"></asp:SqlDataSource>
            </div>
        </form>

    code behind:

    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        Dim rbl As RadioButtonList = TryCast(FormView1.FindControl("rblCheckGoingTo"), RadioButtonList)
        If Not String.IsNullOrEmpty(drv("FruitName").ToString()) Then
            For Each item As ListItem In rbl.Items
                If item.Value = drv("FruitName").ToString() Then
                    rbl.SelectedValue = drv("FruitName").ToString()
                    Exit For
                End If
            Next
        End If
    End Sub

    The result of my work demo: 

     

    If I misunderstanding your question, please feel free to let me know.

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 14, 2019 5:09 AM
  • User1510859543 posted

    Turns out I had to add Trim() to string to match.

    Tuesday, May 14, 2019 1:52 PM