Radio button does not return selected value - c#

i am using radio button list control in asp.net.
i m trying to get selected value on button click Event
but,i m getting Empty string and i want it without javascript.
How can i do this?
<asp:RadioButtonList ID="RadioButtonList1" EnableViewState="true" runat="server"
Width="287px">
<asp:ListItem Value="Single" runat="server" Text="Single"></asp:ListItem>
<asp:ListItem Value="Jointly" runat="server" Text="Married Filing Jointly/Widower"></asp:ListItem>
<asp:ListItem Value="Separately" runat="server" Text="Married Filing Separately"></asp:ListItem>
<asp:ListItem Value="Household" runat="server" Text="Head Of Household "></asp:ListItem>
</asp:RadioButtonList>
C# code
protected void btnCalculate_Click(object sender, EventArgs e)
{
string selectedValue = RadioButtonList1.SelectedValue;
}

When you bind your RadioButtonList, you can place your code in ! IsPostback , in order to don't erase your `selected value, when you post your control (click event).
Page_Load :
if(! isPostBack)
{
//Bind your radioButtonList
}`
Nota : You persist your datas with ViewState

first check your postback event in your page_load..then you can use RadioButtonList1.SelectedValue

Or You could use :--
string selectedValue = RadioButtonList1.SelectedValue.ToString();

Remarks from MSDN for RadioButttonList SelectedValue
This property returns the Value property of the selected ListItem. The
SelectedValue property is commonly used to determine the value of the
selected item in the list control. If multiple items are selected, the
value of the selected item with the lowest index is returned. If no
item is selected, an empty string ("") is returned.
So suggest #1 is that you atleast make on the item as default selection by using the attribute Selected="true"
Suggestion #2 will be (just a opinion) for the sake of readability use SelectedItem.Value
protected void btnCalculate_Click(object sender, EventArgs e)
{
string selectedValue = RadioButtonList1.SelectedItem.Value;
}

Related

Dynamically change radiobuttonlist name in asp.net

In my aspx file, I have a drop down list
<asp:DropDownList runat="server" ID="Dlist" CssClass="dropdown" AutoPostBack="true" SelectedIndexChanged="CtrlChanged">
<asp:ListItem Text="Select item" Value="1"></asp:ListItem>
</asp:DropDownList>
I have a radio button list
<asp:RadioButtonList ID="RadioButtonList1" RepeatColumns="1"
RepeatDirection="Vertical" RepeatLayout="Table" runat="server" AutoPostBack="true">
<asp:ListItem>Option 1</asp:ListItem>
<asp:ListItem>Option 2</asp:ListItem>
</asp:RadioButtonList>
Now I want to change the name of one or both of the radio buttons in the radio button list after something has been selected from the dropdown list using C#. Below is my attempt but not working.
protected void CtrlChanged(Object sender, EventArgs e) {
//attempting to change text of first radio button when item has been selected from dropdownlist
RadioButtonList1.SelectedIndex = 0;
RadioButtonList1.SelectedItem.Text = "Text changed!";
}
First, it is OnSelectedIndexChanged, not SelectedIndexChanged. And the ListItems of a RadioButtonList are index based, so you need to access them like this:
protected void CtrlChanged(object sender, EventArgs e)
{
RadioButtonList1.Items[0].Text = "NewValue 1";
RadioButtonList1.Items[1].Text = "NewValue 2";
}
Your way does change the text, but only for the item you set the SelectedIndex of. And it will change the selected radiobutton to the first one, should one already have been selected.

How to get Dropdowns selected text where there are multiple dropdowns

I have multiple dropdowns like this
<asp:DropDownList ID="ddl1"
runat="server"
DataTextField="Text"
DataValueField="ValID"
AutoPostBack="true"
OnSelectedIndexChanged="ddl_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddl2"
runat="server"
DataTextField="Text"
DataValueField="ValID"
AutoPostBack="true"
OnSelectedIndexChanged="ddl_SelectedIndexChanged">
</asp:DropDownList>
Both the dropdowns has different set of values they are unique. I did the following on ddl_SelectedIndexChanged funtion
DropDownList ddl = sender as DropDownList;
string selectedId = ddl.ID;
string selectedText = ddl.SelectedItem.Text;
When I select values on the first dropdown I get correct selectedId and selectedText.
My Problem:
When I select the second dropdown the selectedId is of the second dropdown but selectedText is always the first dropdowns first value and it never changes. I need selectedText to be that of the Item which I select in the second dropdown.
Any Suggestion?
When binding the data to the DropDownLists, you must place them inside an IsPostBack check.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddl1.DataSource = source;
ddl1.DataBind();
ddl2.DataSource = source;
ddl2.DataBind();
}
}
If you do not the SelectedIndexChanged will fire each time for the DropDownList because rebinding data will trigger that since the previous selecedindex is overwritten.

.net Select only one of two checkboxes?

I have two checkboxes a true and a false. The user should only be allowed to select one, when one is selected the other is deselected and vice versa. I have an on change event which does this, but If i select one so the the other deselects and then press the browser back button they both appear selected?! I put in a method to check for this event, but when they both appeared select on the front end the logic in the back end was seeing one as being unselected? Does this make sense?
My Check boxes:
<asp:PlaceHolder runat="server" ID="phIsValidated">
<asp:CheckBox ID="chbTrue"
Text="True"
runat="server"
AutoPostBack="True"
OnCheckedChanged="Check_Clicked" />
<asp:CheckBox ID="chbFalse"
Text="False"
runat="server"
AutoPostBack="True"
OnCheckedChanged="Check_Clicked" />
</asp:PlaceHolder>
My on changed event:
protected void Check_Clicked(Object sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
if (checkBox.ID=="chbTrue")
{
chbFalse.Checked = !chbTrue.Checked;
}
else
{
chbTrue.Checked = !chbFalse.Checked;
}
}
<asp:RadioButtonList ID="MyRadioButtons" runat="server">
<asp:ListItem Value="Y">Yes</asp:ListItem>
<asp:ListItem Value="N">No</asp:ListItem>
</asp:RadioButtonList>
Here is an example with code to process the selected item:
http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_radiobuttonlist
Step 1: Add two Checkboxes.
Step 2: Add OnCheckedChanged Event to all the checkboxes.
(In case if you do not know How to Add OnChecKedChanged Event, You can follow these steps.
There are two ways to Add this.
1st You can directly add It to your code like
<asp:CheckBox runat="server" ID="Try1ID" OnCheckedChanged="Try1ID_CheckedChanged"/>
Or
2nd You can open the design tab in visual studio and double click on the checkbox you want to create OnChecKedChanged event.
)
Step 3: Set AutoPostBack = True.
(
Example :
<asp:CheckBox runat="server" ID="Try1ID" AutoPostBack="True" OnCheckedChanged="Try1ID_CheckedChanged"/>
or
you can set this from checkbox property window.
)
Step 4 : Add If Else Condition in your .cs file inside the OnChecKedChanged Event
(
Example: If You have two checkboxes with names Try1ID_CheckedChanged and Try2ID_CheckedChanged
Your condition should look like this,
protected void Try1ID_CheckedChanged(object sender, EventArgs e)
{
if (Try1ID.Checked)
{
Try2ID.Checked = false;
}
}
protected void Try2ID_CheckedChanged(object sender, EventArgs e)
{
if (Try2ID.Checked)
{
Try1ID.Checked = false;
}
}
)
Step 5: You Press F5 and test the program.

Listbox always returns 0

I am loading a ListBox OnSelectedChange of DropDownlist. If I select a 3rd value from the ListBox, it always returns 0. What could be wrong? I appreciate any help. Thank you. Here is my code.
<asp:DropDownList ID="dropdown1" runat="server" Width="300" OnSelectedIndexChanged="onChange"
AutoPostBack="true">
<asp:ListBox ID="list1" runat="server" Width="300" Rows="12" CausesValidation="true"/>
protected void OnChange(object sender, EventArgs e)
{
LoadListBox();
}
void LoadListBox()
{
list1.Items.Clear();
System.Data.DataTable rows = new System.Data.DataTable();
rows = DAL.GetValues();
foreach (System.Data.DataRow row1 in rows.Rows) {
list1.Items.Add(new ListItem(row1["measurement"].ToString().Trim(), row1["measurement"].ToString()));
}
}
when you change the listbox value to three this will fire the onchange event
In your onchange event you should could check the value to see what the value has changed to.
in your code you are reloading the list of values in the onchanged event which will reset the selected value back to zero.

On ASP.NET ListView with LinqDataSource, display the data when the search button clicked

I have a ListView setup with LinqDataSource and a button that triggers search function. To avoid display data on page_load, I set ListView's DataSourceID in the Click event of the search button, bind it and set result data in LinqDataSource's Selecting event. It works as I expected but It does't look pretty to set DataSourceId in the button Click event every time the search button is clicked. How can I do this in a better and clearer way?
ASPX code:
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="WebApplication1.DataClasses1DataContext" EntityTypeName=""
TableName="Persons" onselecting="LinqDataSource1_Selecting">
</asp:LinqDataSource>
<asp:ListView ID="ListView1" runat="server" >...</asp:ListView>
<asp:Button ID="Search" Text="Search" runat="server" Click="Search_Clicked"/>
ASPX.CS code:
protected void Search_Clicked(object sender, EventArgs e)
{
ListView1.DataSourceID = LinqDataSource1.ID;
ListView1.DataBind();
}
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
//Search Criteria from CheckBoxList and TextBox applied here.
DataClasses1DataContext data = new DataClasses1DataContext();
var query = from result in data.Persons
where result.ID > 2
select result;
e.Result = query;
}
I honestly don't see anything wrong with your approach, however, if you don't like it, an alternate approach would be to just statically set the DataSourceID in your ListView markup as usual, but set Visible="False", and only make it visible once the button has been clicked.

Categories

Resources