RadioButtonList selected value binding in aspx - asp.net

I got this RadioButtonList with defined ListItems:
<asp:RadioButtonList ID="RblSpouseLocation" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="In Country" Value="0"></asp:ListItem>
<asp:ListItem Text="Outside of Country" Value="1"></asp:ListItem>
</asp:RadioButtonList>
I have a field "SpouseLocation" of type bit in my database. I need to bind the selected value of the RadioButtonList to this field.
I found tutorials of how to do it in the code-behind, but can it be done in aspx ?

I changed my database field type to int and added "SelectedValue='<%# Bind("SpouseLocation") %>" to my radiobuttonlist
The answer to this questions helped me

Related

Validating drop down list

I Have a dropdownlist in an aspx page, I am populating the dropdownlist using a datatable. How can i apply a requiredfield validator to this dropdownlist.? Plz help me
You have to set the InitialValue for the selected item's Value that is selected first:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="DropDownList1"
InitialValue="-- Please select --"
ErrorMessage="Please select something" />
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server">
<asp:ListItem>-- Please select --</asp:ListItem>
</asp:DropDownList>

RequiredFieldValidator for a dropdown list populated by an SQLDataSource

Morning All,
I have a dropdown list that is populated by an SQLSataSource. I wish to make this dropdown list a required field and need to add add a default item like -Please Select- at the top of this list to prompt the user to make a selection.
Here is my code...
<asp:DropDownList ID="ddOwner" runat="server" DataSourceID="OwnersList"
DataTextField="UserFullName" DataValueField="UserFullName" Height="24px"
Width="125px">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvOwner"
ControlToValidate="ddOwner"
ErrorMessage="Please assign an Owner"
Text="*"
runat="server" Display="None"/>
Can i add some code into the 'Initial value' item to set the -Please select- default item?
Regards Betty
Add an unbound item:
<asp:DropDownList ID="ddOwner" runat="server" DataSourceID="OwnersList"
DataTextField="UserFullName" DataValueField="UserFullName" Height="24px"
AppendDataBoundItems="true"
Width="125px">
<asp:ListItem Text="-Please Select-" Value="" />
</asp:DropDownList>
You can try with after your DataBind Method
...
ddOwner.DataBind();
ddOwner.Items.Add("Please select- default item?");

Required Field Validator for drop down list Dynamic

<asp:DropDownList ID="ddTitle" runat="server" DataTextField="TitleName" DataValueField="TitleId"ValidationGroup="t1">
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ID="ReqDropDnw" ControlToValidate="ddTitle" Display="Dynamic" ValidationGroup="t1" InitialValue="<-- Select Title-->" ErrorMessage="Please Select Title">
</asp:RequiredFieldValidator>
I have used this but it's not working, can I have a proper solution for it
My dropdown list is a dynamic.
Use like this way...
<asp:DropDownList ID="ddl" runat="server">
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
<asp:ListItem Text="abc" Value="1"></asp:ListItem>
<asp:ListItem Text="xyz" Value="2"></asp:ListItem>
</asp:DropDownList>
Now you can use required field validator for this dropdownlist.....
After binding data from DB you can use add one empty item to list like:
ddTitle.Items.Insert(0, "");
And now Required field validatior will work for this dropdown.

Uncheched RadioButtonList doesn't fire SelectedIndexChanged

I have a RadioButtonList with some fixed element and a default selected. If I uncheck the selection with javascript, on postback the SelectedIndexChanged event is not fired.
I would expect that the SelectedIndexChanged is called because the index is changed to -1.
this is the Asp.Net code
<asp:RadioButtonList ClientIDMode="Static" ID="RadioButtonList1" runat="server"
onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
</asp:RadioButtonList>
and the jquery code
$('#RadioButtonList1 :radio:visible').attr('checked', false);
As far as I know, the stage of data collection from the submit of the form is handled by IPostBackDataHandler.LoadPostData method.
I derived a class from RadioButtonList to verify when LoadPostData is called and I noticed that when non of the radio button is selected the method is not called and the same for SelectedIndexChanged event.
it seems that if the key of RadioButtonList is not present in the Page.Request.Form.Keys array, the LoadPostData is not called.
Any help appreciated.
Try setting AutoPostBack="true"
Example:
<asp:RadioButtonList ClientIDMode="Static" ID="RadioButtonList1" runat="server"
onselectedindexchanged="RadioButtonList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
</asp:RadioButtonList>
According to MSDN's documentation:
Gets or sets a value indicating
whether a postback to the server
automatically occurs when the user
changes the list selection. (Inherited
from ListControl.)

dropdownlist in gridview null value

I have a dropdownlist in gridview and bind grid view from a function in code behind for the dropdownlist.
The problem is the dropdownlist is in edittemplate and the selected value is the id in the same celle when the is dropdownlist when the row is not editing.
How I can display in editing the null value????
Add an item to your dropdownlist that represent the null value.
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("CurrencyType") %>' AppendDataBoundItems="true">
<asp:ListItem Value="">Not selected</asp:ListItem>
</asp:DropDownList>
You can put Your business logic and check whether the selected value is not null and then you can use your data items that would symbolize null when selected.
<asp:DropDownList ID="ddlitems" runat="server" SelectedValue='<%# Bind("mydata") %>' AppendDataBoundItems="true">
<asp:ListItem Value="">select</asp:ListItem>
</asp:DropDownList>

Resources