0
votes

I have a datagrid with a bunch of rows but only one of them can be the primary so I added a radio button to the datagrid but when I render the page the wrong row is selected. The update SQL works and updates the correct row in the database, but when I render the page the wrong radio button is selected. The value for the selected row has checked=false when I inspect the element and the correct row has a checked=true. How can I get the correct radio button to be selected when I'm already setting the checked value but it looks like it's always setting the selected value to the last row in the grid instead of the one where checked=true.

Aspx radio button column: (label that I'm using as placeholder to group the radio buttons together)

<ItemTemplate>
    <asp:Label ID="rdbPrimary" Text='<%# DataBinder.Eval(Container.DataItem, "Primary")%>' Runat="server" />
</ItemTemplate>

VB datagrid databind:

Protected Sub tblCategories_SelectedIndexChanged(sender As Object, e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles tblCategories.ItemDataBound
    If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
        Dim r As Label
        Dim c As Label
        r = e.Item.FindControl("rdbPrimary")
        c = e.Item.FindControl("lblCategoryID")
        r.Text = "<input type=radio name='myradiogroup' value=" & c.Text & " checked=" & r.Text & " >"
    End If

    If e.Item.ItemType.ToString() = "Footer" Then
        Dim a As Label
        a = e.Item.FindControl("rdbAdd")
        a.Text = "<input type=radio name='myradiogroup'>"
    End If
End Sub
1
You need to use RadioButtonList (or RadioButtons) server control instead of Label. Rendering html mark up from code behind won't work; you won't be able to find them back once the page is posted back.Win
@Win If I use the RadioButton in a datagrid, it names each button uniquely which means you can select every row (defeats the whole purpose). I was using the label in order to set the input name to the same for all rows so it would render where only one value could be selected at a time. r.GroupName doesn't work either because it still renders it with a name like ctl00$ContentPlaceHolder1$tblCategories$ctl02$myradiogroup and the next button is 103, 104 etc.Dave

1 Answers

0
votes

So it turns out that checked=true and checked=false isn't the correct syntax. When building the text for the input it should just be the word checked or blank. So now I'm testing to see if the bound value is true or false and then adding checked or nothing to the string and it's working. Will continue to test but I think this is it.

Aspx page is the same,

VB page additions/updates:

Dim s As String = ""
If r.Text.Contains("True") Then
    s = " checked "
End If
r.Text = "<input type=radio name='myradiogroup' value=" & c.Text & s & ">"

Footer section is the same.