ASP repeater collecting data from radiobuttons - c#

So I have a Asp repeater that prints out some radiobuttons that a user will use to answer a test. What I need to do is to check which radiobuttons are checked so that I can save them to a XML file.
Here is the code
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="repeaterDiv">
<ul id="list">
<li style="list-style: none">
<p style="font-weight: bold">Fråga nummer: <%# Container.ItemIndex + 1 %></p>
<asp:Label ID="q1_label" runat="server" Text='<%#Eval ("text")%>'></asp:Label>
<asp:CheckBox ID="q1_svar1" Text='<%#Eval("q1")%>' runat="server" CssClass="radioButtons;" />
<asp:CheckBox ID="q1_svar2" Text='<%#Eval("q2")%>' runat="server" CssClass="radioButtons;"/>
<asp:CheckBox ID="q1_svar3" Text='<%#Eval("q3") %>' runat="server" CssClass="radioButtons;"/>
<asp:CheckBox ID="q1_svar4" Text='<%#Eval("q4") %>' runat="server" CssClass="radioButtons;"/>
<hr />
</li>
</ul>
</div>
</ItemTemplate>
</asp:Repeater>
I dont really know how to target the checked ones, what do I do from here? Thanks in advance.

You can use foreach loop in repeater items and them user FindControl method
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
CheckBox chk = (CheckBox)item.FindControl("q1_svar1");
if (chk.Checked)
{
//Your code ...
}
}
}

Related

How to get item from listview to disable href

How can i get text of each row from Label1?
My .aspx
<asp:ListView ID="lvRestaurant" runat="server" DataSourceID="ldsOrder" DataKeyName="restaurantID">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="groupPlaceholder" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<GroupTemplate>
<div class="restaurant">
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</div>
</GroupTemplate>
<ItemTemplate>
<div><asp:Label ID="Label1" runat="server" Text='<%# Bind("restaurantID") %>' Visible="false"></asp:Label></div>
<div><asp:Image ID="imgRestaurant" runat="server" ImageUrl='<%# Eval("image", "../resImg/{0}") %>' draggable="false" CssClass="restaurant-img"/></div>
<div class="resName"><b><asp:Label ID="lblName" runat="server" Text='<%# Bind("restaurantName") %>'></asp:Label></b></div>
<div class="deliveryFee"><b>MYR<asp:Label ID="lblDeliveryFee" runat="server" Text='<%# Bind("deliverFee") %>'></asp:Label></b> <span style="color:#C3C3C3;">delivery fee</span></div>
</ItemTemplate>
</asp:ListView>
I try to make if cart exist 1 restaurant product, other restaurant will not be able to click.
My .cs
if(Session["BuyItems"]!=null{
string rID = //get from Label1;
if(rID==Session[existID].ToString()){
orderPage.HreF="#";
}
}
Have a look at foreach, maybe this links can help you.
https://www.codeproject.com/Questions/1029041/How-to-loop-through-a-listbox-items-in-asp-net
How to get all labels and its input elements in javascript
https://www.sitepoint.com/community/t/how-to-get-text-from-a-label-in-a-datalist-itemtemplate/6088/18
Pseudocode
if(Session["BuyItems"]!=null
{
string rID = 5; // for example
foreach (var item in YourListWithRestaurants)
{
if(rID != Session[existID].ToString())
{
// Make the restaurants unable to click
}
}
}
You can do this. Add an OnItemCommand with ListView:
OnItemCommand="ListView_ItemCommand" >
Then you can write an eventhandler:
protected void ListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
Label label1 = (Label)e.Item.FindControl("Label1");
}

ASP.NET button CommandArgument using <% %>

I have the following snippet of code in my .aspx file, I want the button to send the ID to the code behind.
<%List<Notes> CommentsList = FindNotes(ParentNote.ID); %>
<%foreach (var comment in CommentsList) { %>
<div id="note" class="tierTwo"><p><%: comment.Content %></p><ul><li><%: comment.AddedDate %></li><li>20:29</li><li><%: comment.AddedByName %></li><li>Add comment</li></ul> >
<div id="addComment<%:comment.NoteID%>" class="tierOne" style="display:none">
<asp:TextBox ID="commentreplytext" runat="server" rows="4"> </asp:TextBox>
<asp:HiddenField id="ParentIdReply" value='<%=comment.NoteID%>' runat="server" />
<asp:Button ID="Button2" runat="server" Text="Add" CommandArgument="<%: comment.NoteID%>" OnCommand="Add_Comment" />
</div>
</div>
The code behind currently looks like this
protected void Add_Comment(object sender, CommandEventArgs e)
{
string uniqueNoteID = e.CommandArgument.ToString();
}
The command argument is getting sent to the Add_Comment method but it returns "<%: comment.NoteID%>" rather than the value it represents. This way of retrieving the NoteID value works fine when setting the div id so I don't know why it is causing an issue in the commandArgument.
Unfortunately you can't use <%%> code blocks in .NET controls. It's to do with the order things get rendered in. You'll have to set the CommandArgument in the code behind when you build the page. You could do that if you built the page using a repeater rather than a for each in the page.
<asp:Repeater runat="server" id="repeater1">
<ItemTemplate>
<div id="note" class="tierTwo"><p><%# Eval("Content") %></p><ul>
<li><%# Eval("AddedDate") %></li><li>20:29</li>
<li><%# Eval(AddedByName") %></li><li>
Add comment</li></ul>
<div id='addComment<%# Eval("NoteID") %>' class="tierOne" style="display:none">
<asp:TextBox ID="commentreplytext" runat="server" rows="4"> </asp:TextBox>
<asp:HiddenField id="ParentIdReply" value='<%#Eval("NoteID")%>' runat="server" />
<asp:Button ID="Button2" runat="server" Text="Add" CommandArgument='<%#Eval("NoteID")%>' OnCommand="Add_Comment" />
</div>
</div>
</ItemTemplate>
</asp:Repeater>
.cs page:
repeater1.DataSource = FindNotes(ParentNote.ID);
repeater1.DataBind();

asp:repeater items not clearing when new dataSource set

I have a form in which I put one repeater control and bind it on pageLoad,it bind as expected.
I have a dropDownList, when I select a value from it I want to clear the repeater dataSource and rebind it in dropDown_SelectedIndexChanged event handler on codebehind.
I set the repeater datasource as null then rebind it with new datasource it wont reflect the changes, it display the first binded value.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
//Populate the drop down
PopulateLocationDropdown();
if (Request.QueryString["locationID"] != null)
{
int locationID=Request.QueryString["locationID"];
//Clear repeater
rpt_Displaytheater.DataSource = null;
rpt_Displaytheater.DataBind();
//Rebind repeater
rpt_Displaytheater.DataSource = GetTheaterDataSet(locationID);
rpt_Displaytheater.DataBind();
}
}
When dropDown selectionChanged :
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
int locationID=ddlLocation.SelectedValue.ToInt();
//Clear repeater
rpt_Displaytheater.DataSource = null;
rpt_Displaytheater.DataBind();
//Rebind repeater
rpt_Displaytheater.DataSource = GetTheaterDataSet(locationID);
rpt_Displaytheater.DataBind();
}
Edit
here is my dropdown :
<asp:DropDownList ID="ddlLocation" DataTextField="LocationName" DataValueField="Record_Id" runat="server" CssClass="textbox_230"
OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
Here is my repeater :
<asp:Repeater ID="rpt_Displaytheater" runat="server"
OnItemCommand="rpt_Displaytheater_ItemCommand">
<ItemTemplate>
<div class="TheaterListing" style="background: #FFF;">
<div class="TheaterName">
<div class="Theaterhead">
<asp:Label ID="lblTheaterID" runat="server" Visible="false" Text='<%# DataBinder.Eval(Container.DataItem, "Record_Id") %>'></asp:Label>
<asp:LinkButton ID="lnkbtnTheaterName" runat="server" Style="color: #000; font-weight: bold" Text='<%# DataBinder.Eval(Container.DataItem, "Theatre_Name") %>'></asp:LinkButton>
</div>
</div>
<div class="showlist">
<%-- show time repeater-Movies Tab --%>
<asp:Repeater ID="rpt_showtime" runat="server">
<ItemTemplate>
<ul style="padding-left: 10px; margin: -18px 0 0 163px;">
<li class="fl" style="font-weight: bold; padding-left: 8px;">
<asp:LinkButton ID="lnk_showtime" CommandName="Showtime" runat="server" CssClass="txtstyle1"
Text='<%# DataBinder.Eval(Container.DataItem, "Time_From") %>'></asp:LinkButton>
</li>
</ul>
</ItemTemplate>
</asp:Repeater>
<%-- show time repeater-Movies Tab --%>
<%--<asp:LinkButton ID="LinkButton1" runat="server" style="color: #000;font-weight:bold" Text="10.00 AM"></asp:LinkButton>--%>
</div>
</div>
<br clear="all" />
</ItemTemplate>
</asp:Repeater>
My problem is when I reset repeater control's dataSource to new dataSource on dropDown selection changed but the changes is not reflected on the repeater it just show the first bounded values on page load whats wrong with my code
You need to refresh your asp:updatepanel.
This can be achieved by calling yourPanel.Update() or set the UpdateMode of your asp:updatepanel to Always.

How to show or hide a Panel control if a Repeater control's DataSource object contains no data in ASP.NET?

I have a Repeater control that's nested inside a Panel control.
<asp:Panel id="tags" runat="server">
<asp:Repeater ID="rptTags" runat="server" DataSource="<%# this.DataSource %>">
<ItemTemplate>
<span class="tag"><%# Eval("Name") %></span>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>
I set the DataSource attribute of the Repeater control like this DataSource="<%# this.DataSource %>" because this code is actually part of a custom Web Control.
I want to show or hide the Panel control depending on whether the DataSource object has any data in it. I had done it like this in the code-behind:
if (rptTags.Items.Count == 0)
{
tags.Visible = false;
}
The thing is I want to do this in the ASPX part, something like:
<asp:Panel id="tags" runat="server" Visible="<%# ((IEnumerable)DataSource).LengthOrSomething %>">
You could do something like :
C#
public void TagsPreRender(object sender, EventArgs e) {
tags.Visible = (((Repeater)sender).Items.Count > 0);
}
ASPX
<asp:Panel id="tags" runat="server" >
<asp:Repeater OnPreRender="TagsPreRender" ID="rptTags" runat="server" DataSource="<%# this.DataSource %>" >
<ItemTemplate>
<span class="tag"><%# Eval("Name") %></span>
</ItemTemplate>
</asp:Repeater>
</asp:Panel>

Transferring DataBound ID from aspx to codebehind

I have an Unsubscribe page(Unsubscribe.aspx) that generates three radio buttons and a text field by using a repeater.
The id's of the radio buttons come from databinding and I need to get those ID's to use in the code page but I can't seem to get it to recognize the existence of this binding.
Unsubscribe.aspx looks like this:
<asp:Repeater ID="rptReasons" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<div>
<div class="col-lg-12">
<label class="radio-custom">
<input id='rbReason<%# DataBinder.Eval(Container, "DataItem.ID")%>' type="radio" name="rbReason" value='<%# DataBinder.Eval(Container, "DataItem.ID")%>'>
<i class="fa fa-circle-o"></i>
<label for='rbReason<%# DataBinder.Eval(Container, "DataItem.ID")%>'><%# DataBinder.Eval(Container, "DataItem.Explanation")%></label>
</label>
<%# (DataBinder.Eval(Container.DataItem, "ReasonType").ToString() == "OTH") ? "<input class='form-control m-t m-l-n-md' name='txtComment' type='text' maxlength='100' style='width:300px;' />" : "" %>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
And this is the behaviour I want to achieve:
if (DataItem.ID == checked )
{
disable the text area
}
try to add
runat="server"
<input runat="server" id='rbReason<%# DataBinder.Eval(Container, "DataItem.ID")%>' type="radio" name="rbReason" value='<%# DataBinder.Eval(Container, "DataItem.ID")%>'>
after that you need to find control on event
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label lbl = (Label)e.Item.FindControl("Label1");
LinkButton link = (LinkButton)e.Item.FindControl("LinkButton1");
RadioButton rdbtn = (RadioButton)e.Item.FindControl("control_id");
}
}

Categories