Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm making app quiz by asp.net
I have a Repeater control on ASPX-page defined like this:

XML
<asp:Repeater runat="server" ID="rpQuestion" OnItemCommand="rpQuestion_ItemCommand"  >
  <ItemTemplate>
<li id="1" class="items">
 <span class="question"><%#Eval("QuestionName") %></span>
  <div class="btn-group">
<asp:RadioButtonList ID="rpList" runat="server"  RepeatDirection="Horizontal" OnSelectedIndexChanged="rpList_OnSelectedIndexChanged" >
<asp:ListItem Value="1" Text="1" >True</asp:ListItem>
<asp:ListItem Value="2" Text="2"> False</asp:ListItem>
 </asp:RadioButtonList>
 </div>
</li>
 </ItemTemplate>
 </asp:Repeater>



I can't get values of each radiobuttonlist
if can get , value is null
Help me debug this or please give me a simple app about quiz using radiobutton in repeater? thank you
Posted

1 solution

Hello,
Just Refer this Link for get Selected value of radiobutton list inside repeater

http://stackoverflow.com/questions/6702658/get-selected-radio-button-list-value-from-radio-buttons-inside-a-repeater[^]



Check this Code :--
ASP.NET
<asp:Repeater runat="server" ID="rpQuestion">
         <ItemTemplate>
             <li id="1" class="items">
                 <span class="question"><%#Eval("rpQuestion1") %> </br><%#Eval("rpQuestion2") %></span>
                 <div class="btn-group">
                     <asp:RadioButtonList ID="rpList" runat="server" RepeatDirection="Horizontal">
                         <asp:ListItem Value="True" Text="1">True</asp:ListItem>
                         <asp:ListItem Value="False" Text="2"> False</asp:ListItem>
                     </asp:RadioButtonList>
                 </div>
             </li>
         </ItemTemplate>
     </asp:Repeater>

     <asp:Label ID="RadioButtonValue" runat="server"></asp:Label>
     <asp:Button ID="save" runat="server" Text="save" OnClick="save_Click" />


Server side Code :-
C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               DataTable dt = new DataTable();
               dt.Columns.Add("rpQuestion1");
               dt.Columns.Add("rpQuestion2");

               DataRow dr;
               dr = dt.NewRow();
               dr["rpQuestion1"] = "test1";
               dr["rpQuestion2"] = "test2";

               dt.Rows.Add(dr);
               rpQuestion.DataSource = dt;
               rpQuestion.DataBind();
           }
       }

protected void save_Click(object sender, EventArgs e)
       {

           string rdValues = "";
           for (int i = 0; i < this.rpQuestion.Items.Count; i++)
           {
               RadioButtonList rpList = (RadioButtonList)this.rpQuestion.Items[i].FindControl("rpList");
               //this always returns 0
               string value = rpList.SelectedValue;
               RadioButtonValue.Text += value;
           }
       }
 
Share this answer
 
v3
Comments
Tiểu Tử Vdanh 21-Jul-15 2:41am    
Thank you !
I also perform this solution, but it doesn't work
have you got any different solution?
Anil Vaghasiya 21-Jul-15 2:49am    
Hello,
can you display your Server side code???
Tiểu Tử Vdanh 21-Jul-15 2:52am    
This is my code behind
public partial class ListeningSkillsPractice : System.Web.UI.Page
{
readonly ModelDataContext _myModel = new ModelDataContext();
public static string selectedValue = "";

protected void Page_Load(object sender, EventArgs e)
{

rpQuestion.DataSource = _myModel.Questions;
rpQuestion.DataBind();
}

protected void btn_submit_OnClick(object sender, EventArgs e)
{

foreach (RepeaterItem item in rpQuestion.Items)
{
if (item.ItemType == ListItemType.Item)
{

var rb = (RadioButtonList) item.FindControl("rpList");
selectedValue += rb.SelectedValue;
}






}
Response.Write("<Script>alert(" + selectedValue + ")</Script>");
}


protected void rpList_OnSelectedIndexChanged(object sender, EventArgs e)
{
//var rp = (RadioButtonList) rpQuestion.FindControl("rpList");
//selectedValue += rp.SelectedItem.Text;
}
Anil Vaghasiya 21-Jul-15 4:37am    
Hello,

Please refer the below Example...

do this code at server side...!!

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("rpQuestion1");
dt.Columns.Add("rpQuestion2");

DataRow dr;
dr = dt.NewRow();
dr["rpQuestion1"] = "test1";
dr["rpQuestion2"] = "test2";

dt.Rows.Add(dr);
rpQuestion.DataSource = dt;
rpQuestion.DataBind();
}
}
protected void save_Click(object sender, EventArgs e)
{

string rdValues = "";
for (int i = 0; i < this.rpQuestion.Items.Count; i++)
{
RadioButtonList rpList = (RadioButtonList)this.rpQuestion.Items[i].FindControl("rpList");
//this always returns 0
string value = rpList.SelectedValue;
RadioButtonValue.Text += value;
}
}

do this at client Side..!!
<asp:Repeater runat="server" ID="rpQuestion">
<itemtemplate>
<li id="1" class="items">
<span class="question"><%#Eval("rpQuestion1") %> </br><%#Eval("rpQuestion2") %></span>
<div class="btn-group">
<asp:RadioButtonList ID="rpList" runat="server" RepeatDirection="Horizontal" OnSelectedIndexChanged="rpList_SelectedIndexChanged">
<asp:ListItem Value="True" Text="1">True
<asp:ListItem Value="False" Text="2"> False
</div>
</li>


<asp:Label ID="RadioButtonValue" runat="server">
<asp:Button ID="save" runat="server" Text="save" OnClick="save_Click" />
Tiểu Tử Vdanh 21-Jul-15 4:40am    
Ok ,Thank you very much
I will try ,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900