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.
Related
I have a DropDownList that is binded to a DataSource on the PageLoad of a page. In this DropDown List I've added a Select value as the default value. On the bottom of this drop down list and some textboxes I have a Add button. How do I add a validation on the dropdown list so that if the User leaves the value as "Select" he/she can not Click the add button unless he/she selects another value.
<asp:Label ID="lblItems" runat="server" Text="SemesterCode: "></asp:Label>
<asp:DropDownList ID="ddlItems" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text=" -- Select -- " Value="-1"></asp:ListItem>
</asp:DropDownList>
Use the "onchange" method for a drop down list. When the value of the DDL is changed call a JS function that will enable the other buttons/required fields.
function validateThingy(val) {
return (val != -1)
}
function calledOnChange(val) {
if (validateThingy(val)) {
//enable the buttons
} else {
//Keep the button disabled
}
}
Check Selected index of your dropdownlist If it is 0 so alerts a message else puts your logic likewise
if(ddlItems.SelectedIndex==0){
Response.Write("Please choose any value");
}else{
//Put your logic here
}
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.
I have a page with a gridview on it and a dropdown list which controls how many items per page the gridview will display.
The pageSize value of the gridview is controlled by this dropdown list and it gets saved to a cookie.
When the user loads the site the cookie is read so that it remembers what page size the user picked.
I have one problem and that is, if I pick another value on the dropdown list it does not update either cookie or dropdown list. It reverts back to the saved value.
This is the dropdown list created in the gridview pager template:
<PagerTemplate>
<asp:Table ID="Table3" runat="server" Width="100%">
<asp:TableRow>
<asp:TableCell HorizontalAlign="Left">
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
</asp:TableCell>
<asp:TableCell HorizontalAlign="Right" Width="10%">
Page Size
<asp:DropDownList runat="server" ID="ddlPageSize" AutoPostBack="true"
OnSelectedIndexChanged="ddlPageSize_SelectedIndexChanged" OnLoad="ddlPageSize_Load">
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>50</asp:ListItem>
<asp:ListItem>100</asp:ListItem>
</asp:DropDownList>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</PagerTemplate>
and this is where I try to load the value of the dropdown list from cookie:
protected void Page_Load(object sender, EventArgs e)
{
string pageSize = "10";
//Try and load the PageSize cookie from the user's machine and default to 10 records if none is found.
if (Request.Cookies["PageSize"] != null)
{
if (Request.Cookies["PageSize"]["Value"] != null)
{
pageSize = Request.Cookies["PageSize"]["Value"];
int _pageSize;
int.TryParse(pageSize, out _pageSize);
gvRecordsList.PageSize = _pageSize;
DropDownList ddlPageSize = (gvRecordsList.BottomPagerRow).FindControl("ddlPageSize") as DropDownList;
ddlPageSize.SelectedIndex = ddlPageSize.Items.IndexOf(new ListItem(pageSize));
}
}
else
gvRecordsList.PageSize = 10;
if (IsPostBack)
{
ApplyPaging();
}
else
{
gvRecordsList.DataSourceID = "RecordsListSqlDataSource";
gvRecordsList.DataBind();
}
}
The dropdown list index changed code:
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlPageSize = (gvRecordsList.BottomPagerRow).FindControl("ddlPageSize") as DropDownList;
gvRecordsList.PageSize = int.Parse(ddlPageSize.SelectedValue);
Response.Cookies["PageSize"]["Value"] = ddlPageSize.SelectedValue;
Response.Cookies["PageSize"].Expires = DateTime.Now.AddDays(1d);
}
When I step through the code of the SelectedIndexChanged method, I can see that ddlPageSize.SelectedValue always contains the value from the cookie, 50, even though I select another value.
I guess the question is, where do I set the index of the dropdown list?
DropDownList ddlPageSize = (gvRecordsList.BottomPagerRow).FindControl("ddlPageSize") as DropDownList;
ddlPageSize.SelectedIndex = ddlPageSize.Items.IndexOf(new ListItem(pageSize));
The Page_Load event executes before the DropDownList SelectedIndexChanged event. And you are loading the cookie's value to the DropDownList on the PageLoad event.
I suggest you to try loading the cookie afterwards, on the OnPreRender event for example.
Or add a condition to your Page_Load logic, verifying if the PostBack is caused by the DropDownList:
DropDownList ddlPageSize = (gvRecordsList.BottomPagerRow).FindControl("ddlPageSize") as DropDownList;
bool isDDLPostingBack = Request["__EVENTTARGET"] == ddlPageSize.UniqueID;
if (Request.Cookies["PageSize"]["Value"] != null && !isDDLPostingBack)
{
...
}
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 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
{
}
}