Foreach RadioButtons in ASP.NET Webforms - c#

I'm have ASP MultiView for an application form. Inside one of the view I have few radio button as follows--
<div class="row">
<asp:CustomValidator ID="Group1Validator" GroupName="Group1" runat="server"
ValidationGroup="Group1" Display="Dynamic"></asp:CustomValidator>
<asp:RadioButton ID="Group1Yes" runat="server" GroupName="Group1" Text="Yes"/>
<asp:RadioButton ID="Group1No" runat="server" GroupName="Group1" Text="No" />
</div>
Now on the code behind I want to iterate through all the RadioButtons in that page and pull the values to be saved in the DB. I have tried the solution HERE both Loop and LINQ version, but the problem is my RadioButton controls are not directly in the page. Let me elaborate-
First, I have multi view in a page-
Multi view contains views-
And finally inside views I have the RadioButton controls-
Now my goal is the fetch the values of all the RadioButton that are selected which is laid out using the div above.
Any help and guidance will be much appreciated! Thanks!

You must put your RadioButtons as childs of RadioButtonList, then iterate in foreach loop over RadioButtonList items.

Why not use a RadioButtonList
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Text="Yes" Value="1"></asp:ListItem>
<asp:ListItem Text="No" Value="0"></asp:ListItem>
</asp:RadioButtonList>
And then you can get the value in code behind
string value = RadioButtonList1.SelectedValue;
You can also add items to the RadioButtonList in code behind
RadioButtonList1.Items.Insert(0, new ListItem("Yes", "1", true));
RadioButtonList1.Items.Insert(1, new ListItem("No", "0", true));

Related

opposite of selectedvalue in radio button

I'm working in ASP.net and I want to display both values C and F.
This is my default file:
<asp:Label ID="RadioLabel" runat="server" Text="Typ av konvertering"/>
<asp:RadioButtonList ID="RadioButtonList" runat="server">
<asp:ListItem value="C" Selected="true">Celcius till fahrenheit</asp:ListItem>
<asp:ListItem value="F">Fahrenheit till celcius</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="SubmitButton" runat="server" Text="Konventera" OnClick="SubmitButton_Click" />
I use RadioButtonList.SelectedValue in my code behind file to get the value I select.
My question is if there is any way to display the other value that I didn't select?
I'm trying to make a celcius to fahrenheit converter and reverse.
To get all items in RadioButtonList you can do something like this.
foreach (ListItem item in RadioButtonList.Items)
{
// write your code here
}

AJAX Trigger other things to update on click

I have been adding UpdatePanels to my application around the objects that I just want to update.
Example, When I click a button I want a ListBox to update. I would wrap the UpdatePanel around the list box in the html, but when I do this my design goes all over the place.
I am not sure If I'm using AJAX the correct way, as I have seen people adding Triggers but don't know how and where to use them, here is an example of my code:
<asp:ListBox ID="lst_AdminExistingDepartments" runat="server" >
<asp:ListItem Text="Acceptance"></asp:ListItem>
<asp:ListItem Text="Acceptance"></asp:ListItem>
<asp:ListItem Text="Acceptance"></asp:ListItem>
<asp:ListItem Text="Acceptance"></asp:ListItem>
<asp:ListItem Text="Acceptance"></asp:ListItem>
<asp:ListItem Text="Acceptance"></asp:ListItem>
<asp:ListItem Text="Acceptance"></asp:ListItem>
</asp:ListBox>
</div>
<div class="AddEditUpdateDepartment">
<asp:Label ID="lbl_AdminAddEditUpdateDepartments" runat="server" Text="Modify Departments" />
<asp:Label ID="lbl_AdminAddDepartment" runat="server" Text="Add Department" />
<input id="txt_AddDepartment" runat="server" type="text" class="aclass" />
<input id="btn_AddDepartment" runat="server" type="button" class="ButtonAdminPage" value="Add" onserverclick="btn_AddDepartment_ServerClick" />
Here is a ListBox and also a Textbox and a Button. When they click that Button I would want the ListBox to update with the record entered in the TextBox using AJAX?
At the most basic level you need to place the UpdatePanel around all controls being affected by the action. So you need the List, TextBox and Button all inside the UpdatePanel.
Following on from that you have more advanced scenarios where you can have Triggers and multiple UpdatePanels etc. However, it would be best to start with something simple (i.e. everything you need in one UpdatePanel and advance from there.

Property on invisible control is still being bound to

I have a simple radio button list inside a form view:
<asp:RadioButtonList ID="RadioButtonList1" runat="server" SelectedValue='<%# Bind("Answer") %>' Visible="false">
<asp:ListItem Text="Yes" Value="true" ></asp:ListItem>
<asp:ListItem Text="No" Value="false"></asp:ListItem>
</asp:RadioButtonList>
The "Answer" property does not exists, and even though RadioButtonList1 is invisible, it's still being bound to. Why is this? I've tried changing the visibility in various page life-cycle events with no luck.
Any ideas?
Visibility has nothing to do with binding. You would have to remove either the binded property or the button itself for the binding to disappear.

ASP.NET C#, dynamic drop down in repeater causing full post back

I have a page that contains a Repeater, which contains server control elements, within an UpdatePanel and while all other controls behave normally, the Drop Down control causes a full postback every time.
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:SomeWorkingControl ID="swc" runat="server" />
<asp:DropDownList ID="ddl" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="0" Value="0" />
<asp:ListItem Text="1" Value="1" />
</asp:DropDownList>
</ItemTemplate>
</asp:Repeater>
This is vaugely what my code looks like, the DropDownList control is actually in a UserControl, but the theory is the same.
If I apply an event to SomeWorkingControl then there is an Ajax postback and all is fine.
However, the event associated with the DropDownList causes a full postback! I know usually you would set an Async trigger for the DropDown, but since it is created in a repeater (and therefore I can not know how many there will be) so I don't really see how that could work.
Is there anybody who has experienced this before and knows a workaround perhaps?
Try to change this line:
<asp:DropDownList ID="ddl" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged" AutoPostBack="true">
for:
<asp:DropDownList ID="ddl" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged" AutoPostBack="true" ClientIDMode="AutoID">
Recently I had the same problem and I found out that the ClientIDMode can solve it.
Please have a look here: asp.net ClientIDMode Changes

How to align a control side-to-side?

I'm making dynamic radio buttons by using ASP.NET. Where there a validation control that I want to align side of this radios buttons.
Code:
<asp:RadioButtonList
ID="tiposeg"
runat="server"
RepeatColumns="3">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b/asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator15"
ControlToValidate="tiposeg"
ErrorMessage='<% # foo.ErrorMessages.EmptyField %>'
runat="server"
/>
The problem is: if making by using <asp:RadioButtonList, the <asp:RequiredFieldValidator is displayed into next line, unlike if I do it by using "HTML pure", that is displayed side of control (without need to use CSS). I hope this is clear. Thanks in adavnce.
EDIT
in other words, it's possible put the <span> generated by asp:RequiredFieldValidator inside <table> generated by <asp:RadioButtonList?
EDIT 2
My current ASP.NET code;
<asp:RadioButtonList
ID="RadioButtonList1"
runat="server"
RepeatColumns="3">
<asp:ListItem>abcd</asp:ListItem>
<asp:ListItem>xyz</asp:ListItem>
</asp:RadioButtonList>
</li>
<li style="display:inline">
<asp:RequiredFieldValidator
ID="ReqiredFieldValidator1"
runat="server"
ControlToValidate="RadioButtonList1"
ErrorMessage="select atleast one radiobutton!"
/>
The form:
What I'm getting:
What I'm expecting:
Do you really want to arrange the radio buttons in a grid with multiple rows and columns? If not, delete the RepeatColumns attribute and add RepeatLayout="Flow" and RepeatDirection="Horizontal":
<asp:RadioButtonList runat="server" ID="RadioButtonList1"
RepeatLayout="Flow" RepeatDirection="Horizontal">
This will render the radio buttons without a containing <table>, so the validator will appear to the right of the last radio button.
Could you achieve what you want by putting the controls in DIVs and floating them both left?

Categories