opposite of selectedvalue in radio button - c#

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
}

Related

Foreach RadioButtons in ASP.NET Webforms

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));

Getting value from Dropdownlist in Checkboxlist in C#

Ok. I did something crazy. This actually renders correct but how would you get the selected value from the dropdown from the server-side using C#?
I tried getting the dropdownlist code
CheckBoxList.Items[0].Text.Substring(CheckBoxList.Items[0].Text.indexOf("<select>"));
But now that I have the dropdown, how do I get the selected value from it?
EDIT 5/15/15 5:39PM EST
I think it would if I wrote the code as to how I am creating this:
CheckBoxList chkBoxLst = new CheckBoxList();
chkBoxLst.Items.Add("Grade");
chkBoxLst.Items.Add("2");
chkBoxLst.Items.Add("3");
chkBoxLst.Items[0].Text += "<select id='Letter' runat='server'>
<option>A</option>
<option>B</option>
<option>C</option>
</select>"
I am creating this dynamically with server-side code.
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>Grade <select id="Letter" runat="server">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:CheckBoxList>
If you see what I am trying to do and know a better way, suggestions welcome.
If what you are trying to accomplish is get the selected value, change this
<select id="Letter" runat="server">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
for this
<asp:DropDownList ID="Letter" runat="server" >
<asp:ListItem Text="A" Value="A"></asp:ListItem>
<asp:ListItem Text="B" Value="B"></asp:ListItem>
<asp:ListItem Text="C" Value="C"></asp:ListItem>
</asp:DropDownList>
and to get the selected value do this
string selectedValue = Letter.SelectedValue;
You can also get the value from the Form values collection using the id for the SELECT element.
var val = Request.Form["Letter"];

Select an item in a dropdownlist in a listview

I have a listview which is databound by a list of objects.
In the listview, i have a dropdownlist on each item. Which is filled in the .._itemcreated event.
<asp:ListView ID="ListList" runat="server">
<ItemTemplate>
<asp:TextBox ID="ListItem" runat="server" Text='<%# Eval("CompanyName") %>'></asp:TextBox>
<asp:DropDownList ID="ddlAccountManagers" AutoPostBack="True" runat="server" />
<br />
</ItemTemplate>
</asp:ListView>
Depending on which item, i have to set the selectedvalue of the dropdown. But how do I do this?
How do I access the current items values in the itemcreated event?
Since you are able to fill the dropdownlist, I assume you already have access to it.
ddlAccountManagers.Items.FindByText("TextToSelect").Selected = True
or
ddlAccountManagers.Items.FindByValue("ValueToSelect").Selected = True
You could try this one:
ddlAccountManagers.SelectedValue="value you want to be selected"
In the list of objects you have, I suppose that each object will be associated with an AccountManager. An AccoutManager logically would have an id, that will distinguish him/her from the rest of account managers. Then you have to put this value as the selected value.

VS 2005 Webpage. Multi select dropdown list in asp.NET

I have a asp.NET webpage written in VS 2005 (C#). It currently contains a dropdown list. I need to change this dropdown list so that multiple items can be selected. Here is the current code (.aspx page)
<div style="float: left; width: 280px;">
<asp:Label ID="lblExport" EnableViewState="false" runat="server" Text="Export"></asp:Label>
<asp:DropDownList TabIndex="24" runat="server" Width="70px" ID="ddlExport"
DataSource='<%# CodeListManager.Instance().GetCodelist(CodeListCache.Export)%>'
DataTextField="Value" DataValueField="Name" SelectedValue='<%# Bind("Export") %>'
AppendDataBoundItems="true">
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
</div>
Is there a way to keep this as a dropdown or would I need to use a listbox? If I use a listbox how would I program it to allow multi select? Can this be done on .aspx page or will it have to be on the aspx.cs page?

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