Events from inside a DataBound Control - asp.net

I have an asp:Repeater with an asp:DropDownList in it. The DropDownLists in the repeater raise their SelectedIndexChanged events on every postback, regardless of whether they were changed or not.
Is this a known issue in ASP.NET? Do you know how to work around it?
I found this workaround but would like something prettier if at all possible.

Ah I found it - I was adding the event handler in codebehind, but when I moved it to the OnSelectedIndexChanged attribute of the asp:DropDownList control in the ASP.NET markup everything started behaving correctly.

Related

Telerik RadGrid - EditForm events when RadGrid is inside Repeater

I have a Telerik RadGrid which uses the EditFormSettings like this:
<EditFormSettings EditFormType="WebUserControl" UserControlName="~/UserControls/MyUserControl.ascx" >
<EditColumn ButtonType="ImageButton" />
</EditFormSettings>
Within MyUserControl.ascx, I have a button which I'd like to trigger an event handler when clicked. The two ways I know of doing this are as follows:
OnClick="btnOkay_Click" as a button attribute, where the event handler is in the code behind
CommandName="Update" as a button attribute, where this is handled by the event handler passed into the OnUpdateCommand attribute of the RadGrid
Both of these work. However my problem is that they stop working when the RadGrid is within a user control within a Repeater ASP.NET control. In this case, the button event handlers do not get fired (in either of the methods above).
I notice that when I bind my repeater:
rpt.DataSource = Data
rpt.DataBind()
If I wrap that in a if(!Page.IsPostBack), then the edit form doesn't appear at all when I click on the RadGrid row's edit link. So I had to remove that IsPostBack check. I don't think think this is the cause of my problem, as I've tried explicitly not doing this when clicking my update button (by breaking into the debugger). I just thought I'd mention it incase it was related.
Can anyone think of a reason why this would break in a Repeater?
Found the problem with a little help from a work colleague! Turned out to be because I was binding the Repeater in Page_Load rather than Page_Init. Changing it to Page_Init resolved the issue.

Asp.net - button onclick events not raising

I'm a rookie in .net. I'm using an AjaxToolKit Accordion Control and when I put a button in, the "onclick" event is not raising. When I use a dropDownList, if I select "autoPostBack", the event raises normaly (instead, nothing occours). But with buttons I cannot define the "autoPostBack" (its implicit?). It's bringing me several troubles.
Thank you if you can help.
I've discovered the problem (moreover two problems): I'm using AjaxControlToolKit MaskEditExtenders and MaskEditValidators, and a PopUpControlExtender. When the form isn't fulfilled correctly, the MaskEditExtenders/Validators somehow disables form submiting. Also, TargetControlID property of popupControlExtender was set to the button in question. In this case, the event isn't raised.

ASP.NET control event handler not firing on postback?

I have a control which has an ImageButton which is tied to an OnClick event... Upon clicking this control, a postback is performed and the event handler is not called. AutoEventWireup is set to true, and I've double checked spelling etc.... We haven't touched this control in over a year and it has been working fine until a couple of weeks ago.
We have made changes to controls which load this control... so I'm wondering, what kind of changes could we have made to stop this event handler from being called? There is quite a bit of Javascript going on, so this could be the culprit too...
Edit: Some clarification... we are dynamically loading the parent control of the ImageButton in the OnLoad event of the page... if that makes sense.
AutoEventWireup is irrelevant. Is your ImageButton loaded dynamically, i.e. not written out in mark up? If it is loaded onto the page late in the Page lifecycle e.g. in PreRender then the event will not fire.
If there is a JavaScript issue your page will not even PostBack. Is that happening?
Did you give the ImageButton an ID?
I def agree with what BritishDeveloper said. I had a similar problem where I was dynamically loading controls, but I couldn't get a reference to the control using Page.FindControl("controlName") Someone pointed out that I needed to keep the page lifecycle in mind. I found out I needed to make sure to load the control in the PageInit because after doing an async postback the control was still there, but not loaded in the postback so there was no way to find it. This was all in csharp codebehind and ajax though, but I'm guessing the control isn't getting reloaded.
So, as it turns out we set the PostbackUrl property on one of our buttons in control A... this caused the event handlers for control B not to fire when a button in control B was pressed.
If you create a control dynamically. Any time you fire a postback using the new created control, you need to recreated it. Just think that your application are running at a server. How can the server hold information on controls created dynamically? Don't use Page.IsPostBack to create postback. PostbackUrl is bad solution. Workarround will be need.
I can go into a little more detail ... I just lost several hours fixing my own issue similar to the issue described here. In the course of creating some search controls, I added a pair of ImageButtons and set the PostbackUrl properties on them. A few days later while testing new code, I noticed that none of my other buttons on the webform were posting back properly. They would fire a postback, but the code-behind was behaving as if every postback was an initial page request and none of the event handlers were firing.
After several hours of troubleshooting, I came across this post. When I went back and removed the PostbackUrl from those two ImageButtons, everything went back to normal. I don't know why it was causing this issue, but the fix mentioned here worked for me. I want to add that my ImageButtons were not dynamically added ... they were in the markup and this issue still cropped up. Search your markup for any controls with PostbackUrl set ... remove that (program around it if needed) ... see if your event handlers will fire properly.

Repeater ItemDataBound Complete Trigger

Hi I have a Repeater and I am doing various things during the ItemDataBound event. My Repeater is in a control. What I am noticing is that the things that are supposed to happen in the ItemDataBound event happen after the Page_Load of the page hosting the control. Is there a way to use some sort of ItemDataBoundComplete trigger so I can do other things on my page after the events of the ItemDataBound have taken place? Thanks, please let me know if I have not been clear.
[Edit] I have controls that are being bound to the ItemDataBound and they are not available until after the Page_Load for the page hosting the control.
[Solution] (In my case):
In my page I used the following:
Control.Page.LoadComplete += new EventHandler(Control_LoadComplete);
Then I performed what I had to do in that event.
What kind of things? You can override the OnPreRenderComplete method, which is called immediately before the page renders.
You can also change to a ListView, which is support as much flexibility in the html, and use the DataBound event (which is called after the whole ListView has finished data binding).

DropDownList doesn't postback on SelectedIndexChanged

I'm writing an ASP.Net webform with some DropDownList controls on it. Then user changes selected item in one of dropdowns, ASP.Net doesn't seem to handle SelectedIndexChanged event until form is submitted with a 'Submit' button click.
How do I make my dropdowns handle SelectedIndexChanged instantly?
P.S. It's a classic question I have answered too many times, but it seems no one asked it before on stackoverflow.
Setting the AutoPostback property to true will cause it to postback when the selection is changed. Please note that this requires javascript to be enabled.
You need to set the AutoPostBack property of the list to true.
Also, if you're populating the contents of the drop down list from the code behind (getting the contents of the list from a database, for example) - make sure you're not re-binding the data in every postback.
Sometimes people are caught out by binding the drop-down in the page load event without putting it in an If Not IsPostBack. This will cause the event not to fire.
The same is also true of repeaters and ItemCommand events.
if you are populating the dropdown list during page load then each time the page postback it will reload the list thus negating your postback method.
you need to be sure to load the dropdownlist only if (!ispostback)
Set the AutoPostBack property of DropDownList to true.

Resources