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.
Related
I have done some searching on this but i can't seem to find anything related to my issue but how do i get the element Id inside a repeater control? for example i have the following:
<asp:Repeater ID="Rpt" runat="server">
<ItemTemplate>
<p><%# Eval("Name") %>
<asp:HyperLink ID="Url" runat="server" Text ="<%# Eval("Url") %>"/> </span></p>
</ItemTemplate>
</asp:Repeater>
Behind the code i want to get and set Id (url) of the hyper link in code above but for some reason i can't access behind the code as it is not recognised. what is the best way to get the Id?
in my c# code , i just want to set url like so:
Url.NavigateUrl = 'https://stackoverflow.com';
edit
now it doesn't hit the code inside the foreach loop.
foreach (RepeaterItem item in Rpt.Items)
{
HyperLink Url = item.FindControl("Url") as HyperLink;
Url.NavigateUrl = link from db;
}
Many thanks
You can find using following ways:
Using ItemDataBound event:
protected void Rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink hplUrl = (HyperLink)e.Item.FindControl("Url");
hplUrl.NavigateUrl = 'https://stackoverflow.com';
}
}
Or using all rows
foreach (RepeaterItem item in Rpt.Items)
{
HyperLink Url = item.FindControl("Url") as HyperLink;
}
below code can help you:
foreach (RepeaterItem item in Rpt.Items)
{
HyperLink lnk = item.FindControl("Url") as HyperLink;
}
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 already done a lot of research about this here in stackoverflow and in the msdn documentation, however, nothing has helped. I need to be able to select an item in my asp: listview and take that text that I select and put it into a string to be used later. Here's the code of my .aspx:
<li class="menu-item menu-item-has-children">
Undécimo
<ul runat="server" id="eleventhList" class="sub-menu">
<asp:ListView ID="listViewforEleventh" runat="server" OnItemCommand="listViewforEleventh_ItemCommand">
<ItemTemplate>
<a onserverclick="linkForEleven_ServerClick" runat="server" id="linkForEleven" href="ViewSchedule.aspx"> <asp:Label ID="eleventhGroupLabel" runat="server" Text="<%#Container.DataItem %>"></asp:Label> </a>
<asp:Label runat="server" ID="dataString"></asp:Label>
</ItemTemplate>
</asp:ListView>
</ul>
</li>
I'm populating the list view like this:
private void loadEleventh()
{
list = group.getGroupsByLevelService(11);
List<string> sublist = new List<string>();
foreach (var element in list)
sublist.Add(element.GroupName);
listViewforEleventh.DataSource = sublist;
listViewforEleventh.DataBind();
}
This works, but now I need to select the data (text) that is in the sp:ListView. I am doing it like this:
protected void linkForEleven_ServerClick(object sender, EventArgs e)
{
ListViewDataItem item = listViewforEleventh.Items[listViewforEleventh.SelectedIndex];
Label c = (Label)item.FindControl("dataString");
groupName = c.Text;
}
When I debugged my code I got an error of index out of range exception in the selectedIndex method with the value -1. How can I solve this? Or how can I take the data from the listview item and store it in a string in another way?
In your SelectedIndexChanged event handler, do this:
string valueToSave = YourListBox.SelectedValue.ToString();
Of course, you have to make sure that you have actually selected an item in the ListBox in order for there to be a value for YourListbox.SelectedValue. This is just an example of how to do this once. I don't know the details behind how you want to use this value, of course.
I have a dropdownlist inside of a TemplateField, within a GridView.
I would like to dynamically add list items to it and write code to handle when the index changes. How do I go about manipulating the list, since I can't directly reference the DropDownList when it's in a TemplateField.
Here is my code:
<asp:TemplateField HeaderText="Transfer Location" Visible="false">
<EditItemTemplate>
<asp:DropDownList ID="ddlTransferLocation" runat="server" ></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
If I'm understanding what you want to do correctly, you can handle adding items to your drop down like this:
foreach (GridViewRow currentRow in gvMyGrid.Rows)
{
DropDownList myDropDown = (currentRow.FindControl("ddlTransferLocation") as DropDownList);
if (myDropDown != null)
{
myDropDown.Items.Add(new ListItem("some text", "a value"));
}
}
Then, if you mean handling the index change of the DropDownList you just need to add an event handler to your control:
<asp:DropDownList ID="ddlTransferLocation" runat="server" OnSelectedIndexChanged="ddlTransferLocation_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
Then in that event handler you can use (sender as DropDownList) to get whatever you need from it:
protected void ddlTransferLocation_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList myDropDown = (sender as DropDownList);
if (myDropDown != null) // do something
{
}
}
I have an question, On my page i have a ListView control, I also have a button that has CommandArgument. I know i can read and find a control in the ListView as :
ListView.Items[i].FindControl("controlname");
and my button in ListView is like that
asp:Button ID="WisdomButton" runat="server" CommandName="UpdateWisdom" CommandArgument='<%# need my index for Item[i] to post here %>'
OnCommand="UpdateWisdom" Text="Update" />
I want to add index value in runtime to the CommantParameter, so when i go to the Function onCommand i will know exactly from what row[i] i Need to get my controls from ListView.
So my question is, how do i dinamicly add index of ListView.Rows[i] into CommmandArgument for the Button in runtime ?
Thanks in advance.
Check out the API
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewcommandeventargs.aspx
The ListViewCommandEventArgs item has an index, IE it is already available in the argument
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
int i = dataItem.DisplayIndex;
But from here you will have access to those controls
e.Item.FindConrol("controlName");
If you are calling the method a different way you could aways assign the index through an ItemDataBound Event
void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
((Button)e.Item.FindControl("WisdomButton")).CommandArgument = ((ListViewDataItem)e.Item).DisplayIndex;
}
OR try something like this for giggles
<asp:Button runat="server" CommandArgument='<%# DisplayIndex %>'/>
// OR
<asp:Button runat="server" CommandArgument='<%# NextIndex() %>'/>
It might help to know a little bit more about what your are trying to do. If your end goal is to get any of the properties from your bound object you can just cast the dataItem from the ListViewCommandEventArgs of the ItemCommand event. You don't need to retain the index or call FindControl at all.
Here is an example of how to get the bound Customer object.
ListView
<asp:ListView runat="server" id="Customers" ItemCommand="Customers_ItemCommand">
<LayoutTemplate>
<ul>
<asp:placeholder runat="server" id="itemPlaceholder" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:Button runat="server" id="Select" CommandName="Select" />
<%# Eval("Name")%>
</li>
</ItemTemplate>
</asp:ListView>
CodeBehind
public void Page_Load()
{
if (!Page.IsPostBack)
{
this.Customers.DataSource = GetCustomers();
this.Customers.DataBind();
}
}
public void Customers_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
if (e.Item.ItemType != ListViewItemType.DataItem)
return;
var customer = ((ListViewDataItem)e.Item).DataItem as Customer;
if (customer != null)
{
// Now work directly with the customer object.
Response.Redirect("viewCustomer.aspx?id=" + customer.Id);
}
}
}
Edit: In addtion when you cast the item to a ListViewDataItem, then you also expose a ((ListViewDataItem)e.Item).DataItemIndex property that might help you.