0

So. I'm generating a lot of DropDownLists on page. Page has a 2 Views, first has this DDLs that can be changed, second view is doing some other things and user can change this views.

Because DDLs are generated I'm rebuilding them in protected override void OnInit in order to be able see changes in them by user (autopostback on DDLs are disabled).

So now about problem.

Everything is working, but not in totally correct way. If some DDLs are changed and page is starting to reload because of postback (caused by any button click, in this case by button that changes views) SelectedIndexChangedevent is shooting and doing it's things.

Is there a way to make SelectedIndexChanged event shoot only if user presses some particular "save" button and not shooting after presses on any other buttons (that causes postback)?

HoTTab1CH
  • 199
  • 3
  • 20
  • 1
    Can't you just check `Page.IsPostback` in your `SelectedIndexChanged` event handler? – Tamás Szabó May 05 '17 at 08:48
  • 1
    Can't you wrap everything inside an IF logic with Page.IsPostBack and check eventargs or sender? Or when user clicks save you change a boolean CheckDDLs soemthing like that?! – user1845593 May 05 '17 at 08:49
  • Hm, yea, this is good idea. But now how I should force 'SelectedIndexChanged' from button click? Edit: Thx, I'll try – HoTTab1CH May 05 '17 at 08:51

1 Answers1

1

Add if(Page.IsPostback) return; to your SelectedIndexChanged event handler, and call your event handler directly from the button's OnClick method.

The tricky part is you have to make your event handler function without using EventArgs.

Tamás Szabó
  • 1,388
  • 11
  • 23
  • Can you elaborate a bit more how exactly should I call event handler from button? What parameters should I put in it? ddl_SelectedIndexChanged(this, EventArgs.Empty); obviously not working :) I should pass DDL as first parameter? EventArgs can be empty? Because I have hundreds of DDLs :\ So I should call them all somehow in cycle? – HoTTab1CH May 05 '17 at 09:34
  • 1
    You could just call it with `null, null` - if you don't use either the `View` or the `EventArgs`. As to how to call it on all of them, you could add them to a List when you generate them, and then iterate through that. Or if you don't want to store them some reason you can just filter `Page.Controls` for a specific type, [like in this answer](http://stackoverflow.com/questions/7362482/get-all-web-controls-of-a-specific-type-on-a-page). – Tamás Szabó May 05 '17 at 11:30
  • Also, if you specify more what your page does, maybe we can figure out something better. – Tamás Szabó May 05 '17 at 11:34