Datagraid and radiobuttons

  • Thread starter Thread starter SteveS
  • Start date Start date
S

SteveS

This problem is driving me nuts... I have a datagrid which a user can "Approve" or "Deny" a record. I created a radiolist control with "Approve" and "Deny". When the user clicks the submit button, I want to check each row to see which button was checked. For some reason, it doesn't see anything that is checked. This is baffling me. I also tried using the HtmlInputRadioButton control, but that didn't work either. Does anyone have any ideas why the code is never selected?

Thank you in advance!

Here is my code:

ASPX PAGE:

<asp:datagrid runat="server" id="pendingChangesGrid" allowsorting="True" allowpaging="True" autogeneratecolumns="False" onsortcommand="SortGrid" >
<columns>
<asp:BoundColumn DataField="NickName" SortExpression="NickName" ReadOnly="True" HeaderText="Nick Name"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:RadioButtonList Runat="server" ID="rdoList" RepeatDirection=Horizontal>
<asp:ListItem>Approve</asp:ListItem>
<asp:ListItem>Deny</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:datagrid>


ASPX.VB PAGE:

Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
For Each item As DataGridItem In pendingChangesGrid.Items
Dim rbList As New RadioButtonList
rbList = CType(item.FindControl("rdoList"), RadioButtonList)
'THESE ITEMS NEVER SEEM TO BE SELECTED, EVEN THOUGH I SELECT THEM!
Select Case True
Case rbList.Items(0).Selected
'Approved
'...do stuff...
Case rbList.Items(1).Selected
'Denied
'... do stuff...
End Select
Next

End Sub
 
I should have also added that the radiobuttonlist control is on each row in the datagrid. :-)
This problem is driving me nuts... I have a datagrid which a user can "Approve" or "Deny" a record. I created a radiolist control with "Approve" and "Deny". When the user clicks the submit button, I want to check each row to see which button was checked. For some reason, it doesn't see anything that is checked. This is baffling me. I also tried using the HtmlInputRadioButton control, but that didn't work either. Does anyone have any ideas why the code is never selected?

Thank you in advance!

Here is my code:

ASPX PAGE:

<asp:datagrid runat="server" id="pendingChangesGrid" allowsorting="True" allowpaging="True" autogeneratecolumns="False" onsortcommand="SortGrid" >
<columns>
<asp:BoundColumn DataField="NickName" SortExpression="NickName" ReadOnly="True" HeaderText="Nick Name"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:RadioButtonList Runat="server" ID="rdoList" RepeatDirection=Horizontal>
<asp:ListItem>Approve</asp:ListItem>
<asp:ListItem>Deny</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:datagrid>


ASPX.VB PAGE:

Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
For Each item As DataGridItem In pendingChangesGrid.Items
Dim rbList As New RadioButtonList
rbList = CType(item.FindControl("rdoList"), RadioButtonList)
'THESE ITEMS NEVER SEEM TO BE SELECTED, EVEN THOUGH I SELECT THEM!
Select Case True
Case rbList.Items(0).Selected
'Approved
'...do stuff...
Case rbList.Items(1).Selected
'Denied
'... do stuff...
End Select
Next

End Sub
 
This is a bug of sorts arising because the datagrid implements inamingcontainer. The long and short of it is, webserver radiobuttons won't work as intended when embedded in a datagrid. The work around is to implement an html radiobutton and embed it in each row of the datagrid setting it's groupname property appropriately. Finally, to retrieve the selected radio button, you will make a request to the Form passing in the groupname. Here's some sample code to get you going.

[snip] in the itemdatabound event handler
I've added a hyperlink column and I've set it's name to 'Dig'.
Label lbl = (Label)e.Item.FindControl("Dig");

lbl.Text = "<input type=radio name='samegroup' value='somevalue'>";

The result of this code is a radio button rendered in each row of the datagrid with mutually exclusivity. That is, only one radio button can be checked at a time. If you this is not your intention, simply skip the name property.

To retrieve the selected radio button from the datagrid, examine this code:
string str = Request.Form["samegroup"];

str should contain somevalue

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b

I should have also added that the radiobuttonlist control is on each row in the datagrid. :-)
This problem is driving me nuts... I have a datagrid which a user can "Approve" or "Deny" a record. I created a radiolist control with "Approve" and "Deny". When the user clicks the submit button, I want to check each row to see which button was checked. For some reason, it doesn't see anything that is checked. This is baffling me. I also tried using the HtmlInputRadioButton control, but that didn't work either. Does anyone have any ideas why the code is never selected?

Thank you in advance!

Here is my code:

ASPX PAGE:

<asp:datagrid runat="server" id="pendingChangesGrid" allowsorting="True" allowpaging="True" autogeneratecolumns="False" onsortcommand="SortGrid" >
<columns>
<asp:BoundColumn DataField="NickName" SortExpression="NickName" ReadOnly="True" HeaderText="Nick Name"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:RadioButtonList Runat="server" ID="rdoList" RepeatDirection=Horizontal>
<asp:ListItem>Approve</asp:ListItem>
<asp:ListItem>Deny</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:datagrid>


ASPX.VB PAGE:

Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
For Each item As DataGridItem In pendingChangesGrid.Items
Dim rbList As New RadioButtonList
rbList = CType(item.FindControl("rdoList"), RadioButtonList)
'THESE ITEMS NEVER SEEM TO BE SELECTED, EVEN THOUGH I SELECT THEM!
Select Case True
Case rbList.Items(0).Selected
'Approved
'...do stuff...
Case rbList.Items(1).Selected
'Denied
'... do stuff...
End Select
Next

End Sub
 
Hi

Dim rbList As New RadioButtonList '-- Put this above!!
For Each item As DataGridItem In pendingChangesGrid.Items
rbList = CType(item.FindControl("rdoList"), RadioButtonList)
Select Case True
Case rbList.Items(0).Selected
'Approved
'...do stuff...
Case rbList.Items(1).Selected
' Denied
'... do stuff...
End Select
Next

Hope it resolves your problem,

Yama
This problem is driving me nuts... I have a datagrid which a user can "Approve" or "Deny" a record. I created a radiolist control with "Approve" and "Deny". When the user clicks the submit button, I want to check each row to see which button was checked. For some reason, it doesn't see anything that is checked. This is baffling me. I also tried using the HtmlInputRadioButton control, but that didn't work either. Does anyone have any ideas why the code is never selected?

Thank you in advance!

Here is my code:

ASPX PAGE:

<asp:datagrid runat="server" id="pendingChangesGrid" allowsorting="True" allowpaging="True" autogeneratecolumns="False" onsortcommand="SortGrid" >
<columns>
<asp:BoundColumn DataField="NickName" SortExpression="NickName" ReadOnly="True" HeaderText="Nick Name"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:RadioButtonList Runat="server" ID="rdoList" RepeatDirection=Horizontal>
<asp:ListItem>Approve</asp:ListItem>
<asp:ListItem>Deny</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:datagrid>


ASPX.VB PAGE:

Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
For Each item As DataGridItem In pendingChangesGrid.Items
Dim rbList As New RadioButtonList
rbList = CType(item.FindControl("rdoList"), RadioButtonList)
'THESE ITEMS NEVER SEEM TO BE SELECTED, EVEN THOUGH I SELECT THEM!
Select Case True
Case rbList.Items(0).Selected
'Approved
'...do stuff...
Case rbList.Items(1).Selected
'Denied
'... do stuff...
End Select
Next

End Sub
 
Back
Top