I'm having radio button list under repeater.
On Repeaters item data bound I'm binding radio button list but it is binding for every character in datareader.
If I change control to radio button it works fine.
<cms:CMSRepeater ID="rpt_Questions" runat="server" OnItemDataBound="rpt_Questions_ItemDataBound">
<ItemTemplate>
<div><span class="presenter"><%# Container.DataItem%></span></div>
<div>
<%-- <Strong><%# Container.DataItem%></Strong>--%>
<ul class="clearfix">
<cms:QueryRepeater ID="rpt_Answers" runat="server" OnItemDataBound="rpt_Answers_ItemDataBound">
<ItemTemplate>
<asp:RadioButtonList ID="rbtAnswers" runat="server" />
</ItemTemplate>
</cms:QueryRepeater>
</ul>
</div>
</ItemTemplate>
</cms:CMSRepeater>
protected void rpt_Answers_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButtonList rblAnswers = (RadioButtonList)e.Item.FindControl("rbtAnswers");
System.Data.DataRowView drAnswer;
drAnswer = (DataRowView)e.Item.DataItem;
rblAnswers.DataSource = drAnswer["Answer"].ToString();
rblAnswers.DataBind();
}
}
Suppose I am expecting 3 radio button with with values a1,a2,a3
It is creating 6 radio button with
a ,1,a,2,a3
Is this because I am having repeater and then repater and then checkbox list?
protected void rpt_Answers_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButtonList rblAnswers = (RadioButtonList)e.Item.FindControl("rbtAnswers");
System.Data.DataRowView drAnswer;
drAnswer = (DataRowView)e.Item.DataItem;
rblAnswers.DataSource = drAnswer["Answer"];
rblAnswers.DataBind();
}
}
Sudhir's answer might help your case. You might try to check the type of the item first, and add a value if it's the item you looking for.
But from what you provided as a result, I am suspicious that you are actually sending 6 values to the reader. You see, if it creates 6 buttons "1,a,2,a,3,a" it means it gets 6 values.
If that's the case, you should create a list which has "1 a" value together, and send it to the repeater which has the proper areas to show.
Related
I have a radio button list that is inside of a asp repeater. it looks like this:
<asp:Repeater runat="server" ID="surveyRepeater">
<ItemTemplate>
<h3><%#((Half_Blue.Survey_files.survey_classes.surveyQuestion)Container.DataItem).questionNum%>
. <%#((Half_Blue.Survey_files.survey_classes.surveyQuestion)Container.DataItem).question%>
</h3>
<asp:RadioButtonList ID="surveyRadioList"
DataTextField="questionText"
DataValueField="valueOfQuestion" runat="server"
DataSource="<%#((Half_Blue.Survey_files.survey_classes.surveyQuestion)Container.DataItem).answerOptions %>"
RepeatDirection="Vertical">
</asp:RadioButtonList>
</ItemTemplate>
</asp:Repeater>
This displays the radio button list correctly however I cannot figure out how to get the responses in the code behind.
I can get the radio button lists by doing this:
foreach (RepeaterItem item in surveyRepeater.Items)
{
// Checking the item is a data item
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var rdbList = item.FindControl("surveyRadioList") as RadioButtonList;
if (rdbList != null)
{
retList.Add(rdbList.SelectedValue);
}
}
}
however even though it finds all 8 of the radio button lists, the selected value is always just a empty "" string. The selected index is always -1 as well no matter which option I choose.
Any help would be greatly appreciated.
I think you are re-binding the Repeater on every PostBack. You need to wrap it inside an IsPostBack check.
if (!Page.IsPostBack)
{
surveyRepeater.DataSource = Common.LoadFromDB();
surveyRepeater.DataBind();
}
If you do not the RadioButtonLists are recreated and their selection is lost.
I have a normal Repeater Control in my aspx page
<asp:Repeater ID="rpt1" runat="server" OnItemDataBound="rpt1_ItemDataBound">
<ItemTemplate>
<asp:CheckBox ID="chks" runat="server" />
<asp:TextBox ID="txtName" runat="server" CssClass="form-control" Text='<%# DataBinder.Eval(Container,"DataItem.Name").ToString()%>'></asp:TextBox><asp:Label ID="lblValue" runat="server" Visible="false" Text='<%# DataBinder.Eval(Container,"DataItem.Id").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
On the button click I'm binding data to the Repeater as
rpt1.DataSource = GetData();
rpt1.DataBind();
After binding the ItemDataBound event is called. In that I'm looping through the repeater items for some manipulations
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
foreach (RepeaterItem item in rpt1.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
string val = ((Label)item.FindControl("lblValue")).Text;
// Some Stuff
}
}
}
The problem is that the loop is getting started from first every time.
For Ex if my data is as 1 2 3 so on.....
It is iterarting as
1
1 2
1 2 3
How can I make it as
1
2
3
What am I doing wrong
ItemDataBound is already called for every item in the repeater, so you don't need a loop there.
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string val = ((Label)e.Item.FindControl("lblValue")).Text;
// Some Stuff
}
}
Side-Note: that applies to all Data-Bound Web Server Controls.
Okay so imagine next situation:
I have a .ascx file with a repeater and in this repeater there is a asp:hyperlink.
Is it possible to set the url of this hyperlink from the code behind?
In the end the link will always point towards the same url but it needs to be repeated x-times.
test code ascx:
<asp:Repeater runat="server" ID="repeater" OnItemDataBound="repeater_ItemDataBound">
<ItemTemplate>
<div class="container">
<asp:HyperLink ID="hypUrl" runat="server" Text="Dit is een test link"> </asp:HyperLink>
</div>
</ItemTemplate>
</asp:Repeater>
Normally in the code behind I would do something like:
hypUrl.NavigateUrl = Url;
But because the hyperlink is in a repeater it doesnt seem to find the ID.
Anyone knows what the best way is to achieve this?
Thanks in advance!
In repeater's ItemDataBound event. Do it like this
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink hypLink = (HyperLink)e.Item.FindControl("hypUrl");
hypLink.NavigateUrl = "http//www.stackoverflow.com";
}
}
Yes, in your repeater_ItemDataBound event:
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Find the hyperlink
HyperLink hypUrl = (HyperLink)e.Item.FindControl("hypUrl");
// Set the property
hypUrl.NavigateUrl = "foo";
}
}
Maybe a stupid question but I need some help on how to read checkbox values inside a repeater when posting a form. I have a form and inside this form I have a Repeater and in each ItemTemplate I have a CheckBoxList. This is my code a little bit simplified:
<form method="post">
<asp:Repeater runat="server" ID="FormInputValues">
<ItemTemplate>
<asp:CheckBoxList runat="server" ID="CheckBoxValues"
DataSource='<%# ((FormOptions)Container.DataItem).Options %>' />
</ItemTemplate>
</asp:Repeater>
<br />
<asp:LinkButton ID="SelectorNext" CssClass="button" OnClick="SelectorNext_Click"
Text="Next" runat="server" />
</form>
My problem is that I need to be able to map all checked items in each CheckBoxList with its related data item. Something like this:
Dictionary<"DataItem.ID", List<"CheckBox.Value">>
I can't figure out a good way to do this so if someone's got any suggestions I'll be very grateful!
try with the code:
for(int i = 0; i < FormInputValues.Items.Count; i++)
{
CheckBoxList chklist = (CheckBoxList)FormInputValues.Items[i].FindControl("CheckBoxValues");
}
you will get every CheckBoxList in chklist object, you can traverse it to get selected checkboxes.
You can code like below to traverse CheckBoxList:
foreach (ListItem listItem in clbIncludes.Items)
{
if (listItem.Selected) {
//do some work
}
else {
//do something else
}
}
where clbIncludes is a CheckBoxList
You can use foreach loop just inside the for loop to achieve your required thing.
Try this:
Protected function repeater1_ItemCreated(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBoxList checkBoxValues = (CheckBoxList)e.Item.FindControl("CheckBoxValues");
String selectedValue = checkBoxValues.SelectedValue;
//Do your stuff ...
}
}
The ItemCreated event is the one you need, in there you can change how each repeater item looks.
The other answer is also correct, but this example is more performant, as you do not need to loop through all repeater items and you do not need to loop through each checkbox in the CheckBoxList.
I have the following repeater below and I am trying to find lblA in code behind and it fails. Below the markup are the attempts I have made:
<asp:Repeater ID="rptDetails" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><strong>A:</strong></td>
<td><asp:Label ID="lblA" runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
First I tried,
Label lblA = (Label)rptDetails.FindControl("lblA");
but lblA was null
Then I tried,
Label lblA = (Label)rptDetails.Items[0].FindControl("lblA");
but Items was 0 even though m repeater contains 1 itemtemplate
You need to set the attribute OnItemDataBound="myFunction"
And then in your code do the following
void myFunction(object sender, RepeaterItemEventArgs e)
{
Label lblA = (Label)e.Item.FindControl("lblA");
}
Incidentally you can use this exact same approach for nested repeaters. IE:
<asp:Repeater ID="outerRepeater" runat="server" OnItemDataBound="outerFunction">
<ItemTemplate>
<asp:Repeater ID="innerRepeater" runat="server" OnItemDataBound="innerFunction">
<ItemTemplate><asp:Label ID="myLabel" runat="server" /></ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
And then in your code:
void outerFunction(object sender, RepeaterItemEventArgs e)
{
Repeater innerRepeater = (Repeater)e.Item.FindControl("innerRepeater");
innerRepeater.DataSource = ... // Some data source
innerRepeater.DataBind();
}
void innerFunction(object sender, RepeaterItemEventArgs e)
{
Label myLabel = (Label)e.Item.FindControl("myLabel");
}
All too often I see people manually binding items on an inner repeater and they don't realize how difficult they're making things for themselves.
I just had the same problem.
We are missing the item type while looping in the items. The very first item in the repeater is the header, and header does not have the asp elements we are looking for.
Try this:
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{Label lblA = (Label)rptDetails.Items[0].FindControl("lblA");}
Code for VB.net
Protected Sub rptDetails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptDetails.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
Dim lblA As Label = CType(e.Item.FindControl("lblA"), Label)
lblA.Text = "Found it!"
End If
End Sub
Investigate the Repeater.ItemDataBound Event.
You should bind first.
for example)
rptDetails.DataSource = dataSet.Tables["Order"];
rptDetails.DataBind();