I needed to set the AutoPostBack property to true in order for my SelectedIndexChanged event to fire each time a radio button item was selected. Now for some reason if I click the second item or second to last item on the list, that I item never gets selected. The screens flickers; postback, and either the first or last item on the RadioButtonList is selected instead.
Is there a way to handle/prevent this from occurring?
There could be number of reasons why your code acting strange.
You could try following
1) Check if the
You might be missing the OnSelectedIndexChanged attribute in the definition of Radiobutton list.
Add OnSelectedIndexChanged="EventMethod" to RadioButtonList control definition.
2) EnableViewState may set to false on top of the page! Set it to true
Add this line <%# Page EnableViewState="true" >
If none of above work kindly post Your source code so that missing part could be recognized.
I see where the issue is now. Tricky one to find. It appears that because the first two items on my list have the same exact value, when the postback occurs it uses the selected value to set the item. To remedy the issue, I had to add some variation to the values when adding them to the RadioButtonList's list items, so that they are unique. Issue solved! :)
I hope if anyone encountering this issue, this posting helps them out.
Related
This is an odd one. I have a very simple page with a few labels and a single Dropdown List control. This control is an ASP: control. When the user clicks on one of the items in the list, the value selected is not passed back to the server at all. I've tried everything. Enableviewstate, AutoPostBack (sets the index back to 0), an UpdatePanel, and an OnSelectedIndexChanged event (never fires).
I know how this control is supposed to work. While not an ASP.NET expert by any stretch, I've got a good handle on all this. I've never seen a control that didn't fire its event(s) or sent its new value back to the server.
The control is not databound. Rather, it's filled manually within a foreach:
myControl.Items.Add(new ListItem(etc, etc, etc));
Control markup
<asp:DropDownList CssClass="labelDisp"
ID="accountInfoList"
runat="server"
EnableViewState="true"
autopostback="true"
OnSelectedIndexChanged="accountInfoList_SelectedIndexChanged" >
</asp:DropDownList>
Control code-behind
if (lstAccounts != null && !Page.IsPostBack) {
AccountInfo acctInfo = lstAccounts[0];
PopulateFormData(lstAccounts);
}
I found the answer:
It has to do with .Value. If there is more than one ListItem whose .Value is the same, only the first one will be selected. And since .Value didn't change from the initial SelectedIndex, no postback was done.
I happened to recall this because someone had mentioned it in passing a couple months ago. Naturally, no exception is thrown.
When I ask another question sometime, I'll try and provide all the info in the proper format. Thanks to those of you who replied.
I have DropDownList and a TextBox in the EditTemplate of my FormView. All I want is to enable/disable the TextBox based on whether the first entry of my DropDownList is selected:
When the FormView is switched to Edit mode by the user
When user changes the selected item of the DropDownList during Edit mode.
I have achieved the second one through JS and that's working fine, but the first one is proving too difficult. I've tried to do this in ModeChanged event of the FormView, but for some reason the following call returns null in the event:
MyFormView.FindControl("MyDropDownListID");
What am I missing here?
(I'm making sure that MyFormView.CurrentMode is FormViewMode.Edit before making the above call)
One of those times when you pull your hair for hours with a problem, then post it on SO and find the solution in the next few minutes. Anyone else trapped into this, the problem is that the controls of a databound FormView aren't created yet at the time of ModeChanged or Page_Load. You must call the above line in DataBound event and it will work fine.
I wrote code in C# for ASP.Net website , but when i select one item from the list above even doesn't occur and the execution goes back to Site.Master. I have to put a button to fetch the item from the list, how can i make that even occur when i select the item from list?
Assuming you're not using UpdatePanel controls, it should be as simple as setting the AutoPostBack property of your list to true. If this is false then the event won't occur until the form is submitted otherwise.
<asp:ListBox AutoPostBack="true" ...>
In short, my problem is that the WPF DataGrid won't let me select anything other than the first row. When I first fill it with data, no row is selected. Next, no matter what row I click on, the first row lights up. When I bind the SelectedItem property using OneWayToSource, I see that the correct row was indeed selected. If I choose to click again on a different row, nothing happens: the UI remains stuck on the first row and the SelectedItem property retains the previously correct value. At this point, I have to ctrl-click the first row to deselect things. This allows me to repeat the above situation.
In other words:
- the UI seems to be out of sync with what is actually selected.
- Ctrl-click is required to de-select the selection.
- Ctrl-click must be executed on the first row even if the SelectedItem property indicates that a different row is selected.
I understand this is bizarre behavior. I've tried to duplicate it in a separate project with no success. As such, I'm just throwing this out there to see if anyone has any ideas why it might be acting this way?
The only things that I have not duplicated in my separate project is the use of MEF for the View/ViewModel hookup. Everything else is the same.
EDIT: I just replaced said DataGrid with a ListBox and am experiencing the same problem. I'm using Snoop to try to figure out what might be applied to the control that would change its behavior so much.
My problem was that the objects that I was adding to the DataGrid and ListBox had overridden Equals() and GetHashCode() functions. These really screwed up the way both controls rendered which item was selected, therefore explaining the SelectedItem issue. In the end, all the problems were solved by simply removing/fixing those overrides.
Woot!
I'm trying to set the selected value of a ddl during page load ie. before databind.
This causes "selected value does not exist" errors. So I force a databind, and add a new element if it does not exist in the data source.
However it looks like when the databind is performed later in the page lifecycle that my added element(s) are removed/overwritten.
Am I setting the values in the wrong part of the life cycle?
what I'm doing seems rather hackish and I think im going about this the wrong way... is there a better way to do this?
Dont do it on PageLoad do it on the DataBound event of the ddl
Did you consider the OnPreRender event of the DDL... I think you will have everything to set the selected value there
However it looks like when the
databind is performed later in the
page lifecycle that my added
element(s) are removed/overwritten.
That is to be expected, databinding clears out the items and rebinds them again. You should look at what points in the page lifecycle you are calling DataBind and also attempting to set the Selected Value.
Have you considered Page_PreRender to set the SelectedValue? This fires after all the initialisation is done, last thing before the page is rendered to the browser. (Hopefully you won't be doing any databinding in Page_PreRender ;))
But it does not seem very logical to be setting the SelectedValue in one place only for it to be overwritten again, you should only be setting the SelectedValue once - after the final .DataBind()
However it looks like when the
databind is performed later in the
page lifecycle that my added
element(s) are removed/overwritten.
As bgs264 says, that's the behaviour of databinding by design. However, if you set the AppendDataBoundItems attribute to true for your DropDownList, this won't happen, your manually added item will remain in place.
<asp:DropDownList runat="server" id="MyDropDownList" AppendDataBoundItems="true" />
My work around to this solution is as follows:
In Page Load:
Page_Load(..)
{
if(...)
{
hidCGroup.value = objCG.CName;
}
}
In DataBound:
ddlContGroup_DataBound(..)
{
ddlContGroup.Items.Insert(0, "--Select--");
ddlContGroup.SelectedIndex = ddlContGroup.Items.IndexOf(ddlContGroup.Items.FindByText(hidCGroup.Value));
}
Now there are two things to take care of. When you are using FindByText and FindByValue always take special care of the value which you are selecting from the ddl.
Sometimes, we use a numeric item as the DataValue and a text item as the DataText, when that happens, you need to interchange FindByText and FindByValue so that the proper selection is made.
Hope this helps.
ddlExample.SelectedIndex=ddlExample.Items.IndexOf(ddlExample.Items.FindByValue(ExampleID.ToString()));
or
ddlExample.SelectedIndex=ddlExample.Items.IndexOf(ddlExample.Items.FindByText(ExampleText.ToString()));