First, you are missing OnItemCommand="lbSetMemType_Command" in your actual code. In addition, manual repeating with a repeater will give you poor performance, especially if you are only looking for the value of the radio block.
Now let's make the MCVE example together. According to your code, you are trying to build a RadioButtonList inside a Repeater using LinkButton , which use a switch value. In our case, let's just print the value of the specified selected button on the shortcut, confirming our case.
MCVE.aspx
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"> <asp:Repeater ID="rptTest" runat="server" OnItemCommand="rptTest_ItemCommand"> <ItemTemplate> <asp:RadioButtonList ID="rdlTest" runat="server"> <asp:ListItem Text="Annual" Value="Annual"></asp:ListItem> <asp:ListItem Text="Life" Value="Life"></asp:ListItem> <asp:ListItem Text="Installment" Value="Installment"></asp:ListItem> </asp:RadioButtonList> <asp:LinkButton runat="server" ID="lbValidationTest" OnClick="lbValidationTest_Click" runat="server" >Fetch Value</asp:LinkButton> </ItemTemplate> </asp:Repeater> <asp:Label runat="server" ID="lblViewResult"></asp:Label> </asp:Content>
I use the default homepage when creating a new project on VS. It really doesn't matter here.
And codebehind
MCVE.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication { public partial class MCVE: Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { List<int> uselessData = new List<int>(new int[] { 1, 2 }); this.rptTest.DataSource = uselessData; this.rptTest.DataBind(); } } protected void rptTest_ItemCommand(object source, RepeaterCommandEventArgs e) { RadioButtonList list = (RadioButtonList)e.Item.FindControl("rdlTest"); this.lblViewResult.Text = list.SelectedValue; } } }
After some attempts, we clearly see that this is not working properly. There are actually known issues between Repeater and RadioButtonList forbidding us to do just that.
So what to do now? We have many solutions, we can, for example, use javascript to get and set our values ββin a hidden field; we could iterate through the entire repeater tree or check all the buttons or even use the CommandArgument to find the RadioButtonList index and get it in codebehind.
Let javascript solution be implemented. We are going to enter the value of our index in a hidden field present in the repeater. I believe that it is better to relate to this client side, and not to the server side, but I could be wrong and would like to get some result on this.
So add some very dirty javascript code to our view
MCVE.aspx New Version
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"> <asp:Repeater ID="rptTest" runat="server" OnItemCommand="rptTest_ItemCommand"> <ItemTemplate> <div> <asp:HiddenField runat="server" ID="hfSelectedValue" /> <asp:RadioButtonList ID="rdlTest" runat="server" > <asp:ListItem Text="Annual" Value="Annual" onclick="QuickAndDirtyHiddenSetDontUseItInProd(this)"></asp:ListItem> <asp:ListItem Text="Life" Value="Life" onclick="QuickAndDirtyHiddenSetDontUseItInProd(this)"></asp:ListItem> <asp:ListItem Text="Installment" Value="Installment" onclick="QuickAndDirtyHiddenSetDontUseItInProd(this)"></asp:ListItem> </asp:RadioButtonList> <asp:LinkButton runat="server" ID="lbValidationTest" runat="server" UserSubmitBehavior="true" >Fetch Value</asp:LinkButton> </div> </ItemTemplate> </asp:Repeater> <asp:Label runat="server" ID="lblViewResult"></asp:Label> <script> function QuickAndDirtyHiddenSetDontUseItInProd(data) { $(data).parent().parent().parent().parent().siblings("input[name*=hfSelectedValue]").val(data.value); } </script> </asp:Content>
We just changed 3 things. We are going to track a hidden field for each radio bakery list that we are going to create, we added a script to change the value of the specified hidden field and associated two with onclic.
Now, on our code, we just need to track the hidden field, grouped with a click on ButtonLink (i.e. in the same RepeaterItem ), and we are good to go.
MCVE.aspx.cs New Version
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication { public partial class MCVE: Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { List<int> uselessData = new List<int>(new int[] { 1, 2 }); this.rptTest.DataSource = uselessData; this.rptTest.DataBind(); } } protected void rptTest_ItemCommand(object source, RepeaterCommandEventArgs e) { HiddenField hiddenField = (HiddenField)e.Item.FindControl("hfSelectedValue"); this.lblViewResult.Text = hiddenField.Value; } } }
Pretty Self-explainatory, we just find the hidden field in our repeater, and we use it to fill the label.