SelectedIndexChanged event not firing for 1 list item - c#

I have a radiobuttonlist, a label, and a dropdown as follows:
<asp:RadioButtonList id="rbList" runat="server" AutoPostBack="true" EnableViewState="false"
OnSelectedIndexChanged="rbList_SelectedIndexChanged"
RepeatLayout="Table" RepeatDirection="Horizontal" RepeatColumns="3">
<asp:ListItem Selected="True"> Radio 1 </asp:ListItem>
<asp:ListItem> Radio 2 </asp:ListItem>
<asp:ListItem> Radio 3 </asp:ListItem>
</asp:RadioButtonList>
<asp:Label runat="server" ID="lbl" text="1,2" EnableViewState="false"></asp:Label>
<asp:DropDownList runat="server" ID="ddl" Visible="false">
</asp:DropDownList>
My rbList_SelectedIndexChanged is as follows:
protected void rbList_SelectedIndexChanged(object sender, EventArgs e)
{
if (rbList.SelectedIndex == 0 | rbList.SelectedIndex==1)
{
lbl.Text = "1,2";
ddl.Visible = false;
//ddl.Attributes.Add("style", "display:none");
}
else if (rbList.SelectedIndex == 2)
{
lbl.Text = "3";
ddl.Visible = true;
//ddl.Attributes.Add("style", "");
}
}
Now when I change from radio3 to radio2, the event is getting fired as usual and everything looks good. But when I change from radio3 to radio1, I don't see the event getting fired (I inserted a breakpoint) the ddl stays visible but the value of lbl changes to 1,2.
My 2 questions are as follows:
1) Why is the event not getting fired on changing from radio3 to radio1?
2) How is the label value getting changed when the event is not firing?
Any help or comments are much appreciated..Thanks in advance!

I am not sure this is a bug or not, but...
When EnableViewState="false" with DDL or RBL and user trying to pick the first list item (index 0), the SelectedIndexChanged will NOT get fired.
If you set EnableViewState="true", then the DDL or RBL should work properly when user pick the first list item while non-first item was selected...

Preselecting a radio button in your markup is causing your problems. going from any other option back to option 1 will not trigger the changed event.
this line is your culprit.
<asp:ListItem Selected="True"> Radio 1 </asp:ListItem>
if you remove the Selected attribute the event should register properly
<asp:ListItem> Radio 1 </asp:ListItem>
you could handle the preselection in your code behind.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
rbList.SelectedIndex = 0;
}
}

As far as I can tell, programmatically setting SelectedIndex (even on first post) results in the same behaviour as setting Selected="True" on the markup.
The only sure-fire way seems to be using an UpdatePanel with the RadioButtonList as an async trigger, making sure the markup changes with every change.
That is, unless you want to go the jQuery route..

I had a similar problem, but it had to do with the "ChildrenAsTriggers" property of the update panel being set to false. All other radio button indexes worked fine this way, except index 0.

Related

How to make dropdownlist item's unselectable,once it has been selected

<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
I have one dropdownlist in my form in .aspx File.
Once I select "1", then next time if I redirect to same page then "1" should be
unselectable or Hidden for making sense that I have selected "1" previously because I have large items in my example.
Disabling any particular item in a dropdownlist is not possible.
Alternate:
You can use a BulletedList Server Control and use its Enable = False property to disable any particular item and all user can see that item as disabled.. here is a design time example..
<asp:BulletedList ID="BulletedList1" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem Enabled="False">3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:BulletedList>
Took from here
You can use a hidden field to store which item was selected in code behind and disable that item using the following code.
//Code Starts
$(document).ready(function() {
$('#ddlList option:contains("HTML")').attr("disabled","disabled");
});​
//Code Ends
You can track your selection using Cookies,
on the "onChange" event of your dropdown list, create a cookie with the selected value,
then on body "onLoad" u can check and disable the items that are there in Cookie.
Or you can hold your selected values in a hidden field
To remove the last selected item in your dropdownlist (DDL), considering the following code for your DDL form, you can use the onselectedindexchanged event of your DDL (that is raised when you click on one of your DDL items):
<asp:DropDownList ID="_DDL" runat="server"
onselectedindexchanged="_DDL_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
And put on the code behind the RemoveAt() method to remove the selected Item from your DDL:
protected void _DDL_SelectedIndexChanged(object sender, EventArgs e)
{
int ItemToRemove = _DDL.SelectedIndex;
_DDL.Items.RemoveAt(ItemToRemove);
}
Hope this will be helpful :)

How to access a control in an .aspx page [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
I attach event with radiobuttonlist in code behind page, radiobuttonlist is inside a
listview .When I run program it generates an error :
"object referance not set to instant of object"
.aspx code:
<asp:ListView ID="ListView1" runat="server" >
<ItemTemplate>
<tr><td>
<asp:RadioButtonList ID="radiobuttonlist4" runat="server" AutoPostBack="true"
RepeatDirection="Horizontal"
OnSelectedIndexChanged="selected" Width="240px">
<asp:ListItem Value="agree"></asp:ListItem>
<asp:ListItem Value="disagree"></asp:ListItem>
<asp:ListItem Value="strongagree"></asp:ListItem>
<asp:ListItem Value="strongdisagree"></asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
.aspx.cs Code
assessdal s = new assessdal();
ListView1.DataSource = s.showop1();
ListView1.DataBind();
RadioButtonList list= ListView1.FindControl("radiobuttonlist4") as RadioButtonList;
list.SelectedIndexChanged += new EventHandler(handle);
public void handle(object sender, EventArgs e)
{
Label2.Text = "y";
}
First, i fixed a ton of typos in your code.
Second, it's not finding it because FindControl is being called on ListView1, not the page (or the control hierarchy in which it exists) and FindControl only looks within that instance's child controls.
Try Page.FindControl("radiobuttonlist4") to find it in the page.
You should attach the event handler declaratively on the aspx, that's the easiest way.
<asp:RadioButtonList ID="radiobuttonlist4" runat="server" AutoPostBack="true"
RepeatDirection="Horizontal"
OnSelectedIndexChanged="selected"
Width="240px">
</asp:RadioButtonList>
Since the ListView can contain multiple items, the NamingContainer of a control in it's Itemtemplate is not the ListView, but the ListViewItem. That ensures that every control gets a unique id on clientside.
So you can find your RadioButtonList in the button's click event handler in this way:
var button = (Button)sender;
var item = (ListViewItem)button.NamingContainer;
var radiobuttonlist4 = (RadioButtonList)item.FindControl("radiobuttonlist4");
If you want to "find" the RadioButtonList in it's SelectedIndexChanged event, simply cast the sender argument accordingingly (var rbl = (RadioButtonList)sender;).

About SelectedIndex property in a DropDownList

Sorry for my ignorance, but I've always believed that setting the SelectedIndex property of a DropDownList the SelectedIndexChanged event is fired... Am I wrong?
Searching in the documentation I haven't found anything clear...
Thanks
No it cannot fire the selectionchange event.
if you are settting programatically than it not fire the selectionchange event.
DropDownlist Class Selected Index Changed Event fires when the selection from the list control changes between posts to the server.
Please make sure about following to fire the event.
AutoPostBack="true" How to use DropDownList AutoPostBack feature
onselectedindexchanged="Name of the Handler"
Below is the code.
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true"
onselectedindexchanged="ddl1_SelectedIndexChanged">
<asp:ListItem Text ="1" Value="1"></asp:ListItem>
<asp:ListItem Text ="2" Value="2"></asp:ListItem>
</asp:DropDownList>
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Note - On setting the index at runtime will never fire the event.
Please go through the following links as well.
How to: Respond to Changes in List Web Server Controls
How to use DropDownList AutoPostBack feature

Response.Redirect Same Page RadioButtonList SelectedItem

I'm trying to do a job filter for the list of jobs on our website. The filter for the job type is wrapped in an UpdatePanel and the button to apply the filters redirects back to the same page.
This is because I will be using the umbraco.library:RequestQueryString in the XSLT to populate the jobs list.
However, the querystring filter value doesn't seem to select the RadioButtonList. For example:
The page loads, but nothing happens because vt is null:
protected void Page_Load(object sender, EventArgs e)
{
string vt = Request.QueryString["vt"];
if (vt != null)
{
foreach (ListItem li in rblVacancyType.Items)
{
if (li.Value == vt)
{
li.Selected = true;
}
}
}
}
<asp:UpdatePanel ID="upSearchFilters" runat="server">
<ContentTemplate>
<p>
<asp:RadioButtonList ID="rblVacancyType" runat="server">
<asp:ListItem Text="All" Value="all"></asp:ListItem>
<asp:ListItem Text="Permanent" Value="permanent"></asp:ListItem>
<asp:ListItem Text="Temporary" Value="temporary"></asp:ListItem>
</asp:RadioButtonList>
</p>
</ContentTemplate>
</asp:UpdatePanel>
Here's the button:
<asp:ImageButton ID="ibFilters" ImageUrl="~/images/buttons/filter-button.png" OnClick="ibApplyFilters_Click" runat="server" />
Here's the procedure:
protected void ibApplyFilters_Click(object sender, EventArgs e)
{
Response.Redirect("/careers/join-us/?filters=true&vt=" + rblVacancyType.SelectedValue.ToString());
}
Yet when the page redirects the first time, nothing is selected, I click permanent, permanent gets selected. If I then select 'All' or 'Temporary' the selection doesn't change.
Any ideas?
Based on the behavior (it works the first time) I believe this describes what's happening:
MyPage.aspx (original load)
Page controls initialized to default
Page Load - No query string, no radio button selected
(user clicks button - causes postback)
MyPage.aspx (postback)
Page controls initialized to default
Radio Button Set from ViewState
Page Load - No query string, no radio button selected
ButtonClick - uses radio button setting, does response redirect
MyPage.aspx?VT=Permanent (load from redirect)
Page controls initialized to default
Page Load - Query string set, radio button selected
(user clicks button - causes postback)
MyPage.aspx?VT=Permanent (postback)
Page controls initialized to default
Radio Button Set from ViewState
Page Load - Query string set, radio button set to Permanent (Here is the problem)
ButtonClick - uses radio button setting, does response redirect
I believe a simple (if !IsPostback) will fix things
Sounds like postback values being re-applied. See this article on ASP.NET page lifecycle: http://msdn.microsoft.com/en-us/library/ms178472.aspx.
Values for controls are re-applied via postback after page_load.
Due to the strange nature of code + postbacks the logic in Mark's answer seems to be quite accurate, but the suggested fix did not work as I had tried that as a possible solution. The below is a modification, but as far as I could see it works. Give it a try, it might work out for you.
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="upSearchFilters" runat="server">
<ContentTemplate>
<p>
<asp:RadioButtonList ID="rblVacancyType" runat="server"
AutoPostBack="True">
<asp:ListItem Text="All" Value="all"></asp:ListItem>
<asp:ListItem Text="Permanent" Value="permanent"></asp:ListItem>
<asp:ListItem Text="Temporary" Value="temporary"></asp:ListItem>
</asp:RadioButtonList>
</p>
<p>
<asp:ImageButton ID="ibFilters" runat="server" CausesValidation="False"
Height="30px" OnClick="ibApplyFilters_Click" />
</p>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
ibFilters.PostBackUrl = "~/WebForm1.aspx?filters=true&vt=" + rblVacancyType.Text;
string vt = Request.QueryString["vt"];
}
Important:
The way this is set up, it will maintain your selection, update the filter parameters in the url on button push, and then assign vt the correct value to be used on filtering anything else on your page.
You must change out my "~/WebForm1.aspx?filters=true&vt=" for your url as well.

RadioButtonList OnSelectedIndexChanged

I'm looking for the best way to handle a change of index being selected on a ASP.net RadioButtonList (C# code behind). I have 3 list items. For the first one, I want it to show a hidden asp:textbox on the page, whereas the other 2 will hide the textbox.
//asp.net side
<asp:RadioButtonList ID="_indicatorAckType" runat="server" RepeatDirection="Horizontal"
enabled="true" OnSelectedIndexChanged="onAckTypeChanged">
<asp:ListItem Text="None" />
<asp:ListItem Text="SHOW" />
<asp:ListItem Text="HIDE" />
</asp:RadioButtonList>
//code behind
protected void onAckTypeChanged(object sender, EventArgs e)
{
if (_indicatorAckType.SelectedItem.Text == "SHOW")
_myTextboxID.Visible = true;
else
_myTextboxID.Visible = false;
}
I initially tried using onclick event handlers, but I've been told that ListItem's cannot use onclick events with radio button items. What am I doing wrong here? This does not throw any errors or have any visibly obvious problems. I have tried making onSelectedIndexChanged do nothing except show the textbox and that doesn't work either.
Any help is appreciated! Thanks everyone.
On the RadioButtonList, set the AutoPostBack attribute to true.
Have a look at this it might help. And I suggest to turn off autopostback if enabled on radio button do it all on client side using jquery.
example:
Using jQuery, hide a textbox if a radio button is selected

Categories

Resources