Uncheched RadioButtonList doesn't fire SelectedIndexChanged - asp.net

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

Related

RadioButtonList selected value binding in aspx

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

Postback of one dropdown fires selectedindexchanged event of all all remaining dropdown

Example:
<asp:DropDownList runat="server" AutoPostBack="true" ID="ddlSample">
<asp:ListItem Text="text1" />
<asp:ListItem Text="text2" />
</asp:DropDownList>
Now if change the item in the above dropdown it will cause all the other dropdowns postback events to fire.
Like
<asp:DropDownList EnableViewState="true" ID="ddlNewSelec" runat="server"
AutoPostBack="true" OnSelectedIndexChanged="ddlNewSelec_SelectedIndexChanged">
<asp:ListItem Text="--Select Test Type--" Value="" />
</asp:DropDownList>
Details:
The Page has ViewState disabled. I try enabling viewstate for the individual dropdowns but it does not matter.
Any idea why this is happeing.
Let me know if you need any more details
EDIT:
The dropdown fires the selectedindexchanged event of all other dropdowns on the form. The dropdown named as ddlSample is doing nothing just causing postback but why the selectedindexchanged evenet of other dropdowns fired up.
Additional Info:
All the inputs are in updatePanel and removing them does not make any effect.
In Page Load i am not doing anything on postback
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindSomeDDLs();
}
}
You didn't bind the SelectedIndexChanged event on the "ddlSample" dropdown. Please try binding that property like so..
<asp:DropDownList ID="ddlSample" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSample_SelectedIndexChanged">
<asp:ListItem Text="text1" />
<asp:ListItem Text="text2" />
</asp:DropDownList>
The answer seems to be very weird but this is true.
This is happening just because of EnableViewState property set to false.

Initial SelectedValue in DropDownList not posting back when re-selected

I have a DropDownList with the following markup:
<asp:UpdatePanel id="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Always">
<ContentTemplate>
<asp:DropDownList id="ddlCampaignModule" runat="server" OnSelectedIndexChanged="ddlDynamicType_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="-1">None</asp:ListItem>
<asp:ListItem Value="10">Category Menu</asp:ListItem>
<asp:ListItem Value="11">Best Sellers</asp:ListItem>
<asp:ListItem Value="12">Best Reviews</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
In the code behind I select the second option Category Menu. If I run my code and select any other option it posts back to ddlDynamicType_SelectedIndexChanged however if I re-select the second option (after selecting one of the other ones) my postback isn't triggered.
Am I missing something simple here?
Sounds like you are always selecting the 2nd option in the code behind, regardless of postback... ensure you only do that if it isn't!
if (!IsPostBack)
{
//select 2nd Item
}

Page refresh on clicking checboxlist in repeater

I am annoyed with a problem I have repeater control in updatepanel like this.
<asp:UpdatePanel ID="UpdPnlConstituentRepeater" ChildrenAsTriggers="true" runat="server">
<ContentTemplate>
<asp:Repeater ID="repConstituentInformation" runat="server" OnItemDataBound="repConstituentInformation_ItemDataBound">
<ItemTemplate>
<asp:DropDownList ID="dropRegistrantDownCostType" runat="server" AppendDataBoundItems="true"
AutoPostBack="true" OnSelectedIndexChanged="dropRegistrantDownCostType_SelectedIndexChanged"
EnableViewState="true">
<asp:ListItem Text="Select Type" Value="0" Selected="True" />
</asp:DropDownList>
<asp:CheckBoxList ID="chkBoxListRegistrantBenefits" AutoPostBack="true" runat="server"
OnSelectedIndexChanged="chkBoxListRegistrantBenefits_SelectedIndexChanged">
</asp:CheckBoxList>
</itemTemplate>
</ContentTemplate>
</asp:UpdatePanel>
The problem I face that whenever I select any value from dropdown all the page gets refreshed After spending hours on the google I found a solution i.e, on itemdatabound event of repeater we just need to add the following code after finding the dropdown,
Dim sm As ScriptManager = ScriptManager.GetCurrent(Page)
sm.RegisterAsyncPostBackControl(objDropdownlist)
It worked very well for the dropdown but same is not working for checkboxlist like using the scripmanager instance if I write sm.RegisterAsyncPostBackControl(chkBoxListRegistrantBenefits), it is not working :(
Add ClientIDMode="AutoID" to the Repeater control.
You should not have to add any triggers and you should not even have to call RegisterAsyncPostBackControl. The ChildrenAsTriggers should take care of that.
on Repeater ItemDataBound event
use screiptmanager method RegisterAsyncPostBackControl
this.ScriptManager1.RegisterAsyncPostBackControl(
e.Item.FindControl(" put here your checkbox ID "));
Add this Before start of <ContentTemplate>:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="objDropdownlist" EventName="OnSelectedIndexChanged" />
</Triggers>

Required Field Validator gets run on post back trigger

I have a dropdown list on my aspx page on which I have a RequiredFieldValidator applied over it. DropDown code is:
<asp:DropDownList ID="ddlglCategoryId" runat="server" CssClass="textEntry2"
AutoPostBack="true" ValidationGroup="Save" DataSourceID="dtsglCategoryId" DataTextField="LookupItem"
DataValueField="Id" AppendDataBoundItems="true">
<asp:ListItem Text="All" Selected="True" Value="0"></asp:ListItem>
</asp:DropDownList>
RequiredFieldValidator code is:
<asp:RequiredFieldValidator ID="rfvddlglCategoryId" InitialValue="0" runat="server"
ErrorMessage="Please select category" CssClass="Validations" ControlToValidate="ddlglCategoryId"
ValidationGroup="Save" Display="Dynamic" SetFocusOnError="true">
</asp:RequiredFieldValidator>
I have a post back trigger as well on my aspx page and when I change the category from the dropdown, page posts back and a grid on my page gets updated. But due to post back, the validator message appears and then disappears. I want this to appear only when "All" is selected from dropdown and user clicks save button.
Any guidelines?
Trigger:
</ContentTemplate>
<Triggers>
<%--<asp:AsyncPostBackTrigger ControlID="lbFileName" />--%>
<asp:PostBackTrigger ControlID="btnFileUploadSave" />
</Triggers>
</asp:UpdatePanel>
Button:
<asp:ImageButton ID="btnFileUploadSave" runat="server" ValidationGroup="Save"
ImageUrl="~/App_Themes/Default/images/update.png" ToolTip="Save"
Height="18px" onclick="btnFileUploadSave_Click"/>
You may need to set the AutoPostBack property of the dropdownlist to false. It sounds like a postback is getting fired with the OnSelectedIndexChanged event handler.
Set the ValidationGroup Property Of all Validaters same which you want to validate on button click and then set buttons ValidationGroup property same as Your Validators
For Example:
<asp:RequiredFieldValidator ID="rfvDoc" runat="server" ControlToValidate="FileUpload1"
ErrorMessage="Please Select A Document" ValidationGroup="OnSave">
</asp:RequiredFieldValidator>

Resources