0

There is an event onSelectedIndexChange for DropDownList in ASP.NET which triggers when a selected index is changed for a dropdown.

I have encountered a situation where I need to trigger similar kind of event when SelectedIndex of DropDown does not change upon selection.

I am totally puzzled what to do in such a case?

Any help/references will highly be appreciated.

Thank you.

SHAKIR SHABBIR
  • 1,295
  • 1
  • 14
  • 25
  • Can you please elaborate such condition. I am unable to imagine any such scenario. – Murtuza Kabul Oct 01 '12 at 10:25
  • Okay. Its a scenario, where select an item from drop down and it becomes a linkbutton displaying the selected item and the drop down goes visible false. Then, when the link button is clicked, the link button goes hidden and drop down goes visible with selected item that was last selected. The cycle continues. My problem comes when the same index is selected so no event is fired and the dropdown remains intact. – SHAKIR SHABBIR Oct 01 '12 at 10:29

4 Answers4

0

assign an a event handler to the selected index changed event and set autopostback to true in markup

or code behind

mydropdownlist.SelctedIndexChanged += NameOfMethod

the handler is then defined like this

protected void NameOfMethod(object sender, EventArgs e)
{
     //your code here
}

update by definition the selectedindexchanged event would only fire when the index changes. if you want to force the postback that will require some javascript. here is an example of how to do that with jquery

$(function() {
    $('select').change();
});
Jason Meckley
  • 7,589
  • 1
  • 24
  • 45
  • but how do I assign such an event, autopostback is alsready set to true. – SHAKIR SHABBIR Oct 01 '12 at 10:26
  • Yes, nice but thats will trigger only when selectedindex is changed. I want to trigger an event when the selection has been made and the selected index is not changed. That;s the point! – SHAKIR SHABBIR Oct 01 '12 at 10:38
0

Assuming you have another server-side control on your page that causes a postback, you could write a routine in the postback event for the other control that compares the current selection with the previous selection and fire a custom-event (or the routines you want to happen) if the value has not changed.

That said, I have to imagine there's an easier way to accomplish the overall goal you're trying to achieve, but you'll have to be a little more specific in your question.

UPDATE

I have to assume that you are using the value from the dropdown when you are processing the form. Why not start off with the dropdown hidden and the linkbutton shown? Just select a default from the dropdown list and allow the user to change it as needed.

Here's a fiddle showing that behavior: http://jsfiddle.net/rjaum/

pete
  • 24,141
  • 4
  • 37
  • 51
  • "Another server side control that causes a post-back" but when will it post back that's the point. Well see my next comment for more specific view of my problem. Thank you for your response though! – SHAKIR SHABBIR Oct 01 '12 at 10:42
  • Okay. Its a scenario, where select an item from drop down and it becomes a linkbutton displaying the selected item and the drop down goes visible false. Then, when the link button is clicked, the link button goes hidden and drop down goes visible with selected item that was last selected. The cycle continues. My problem comes when the same index is selected so no event is fired and the dropdown remains intact. – – SHAKIR SHABBIR Oct 01 '12 at 10:43
0

I think that is normal. The event is SelectedIndexChanged and you said you selected the same item that was previously selected before. So the index remains the same, not changed, and the event won't fire. May be you look at OnClick.

The issue is that you have not changed the index when you clicked the second time, so the dropdown is still waiting for you to change it

codingbiz
  • 26,179
  • 8
  • 59
  • 96
  • That;s right but I have a situation where I have to trigger the event every time the selection has been made, regardless of the fact that the selected index was changed or not. Do you have an answer to this? – SHAKIR SHABBIR Oct 01 '12 at 10:39
0

That's fairly easy. You can achieve this using javascript/jquery/server side code etc. Assuming user does click the control. Something like this on pageLoad

PageLoad()
{
       YourDropDownList.Attributes.Add("onclick","javascript:CallHelloWorld();return false;");
}

Then on server side you can decorate a method with WebMethod attribute

[WebMethod()]
public static string HelloWorld()
{
      return "Hello foo";
}

On your client side aspx you can use jQuery to call your webmethod

<script language="text/javascript">
 function CallHelloWorld()
 {
     // Call HelloWorld webmethod using jQuery $.ajax
 }
 </script>

Edit You can use a radiobutton list instead of dropdownlist. That way, on client side you can check the event when the radio button is clicked that it is checked or not (if its checked fire your event). Edit Also try looking at this thread if you want to use dropdown list specificallyFire event each time dropdown list is selected with JQuery

Community
  • 1
  • 1
Zo Has
  • 12,599
  • 22
  • 87
  • 149
  • Thank you Damien! But the problem is onclick event will fire just when the dropdown is clicked even before any item has been selected. What I want is to trigger the event when an index is selected! – SHAKIR SHABBIR Oct 01 '12 at 11:09
  • I would be glad to mark the best one as the answer bu the problem is that my issue is still to be solved. Please carefully go through the comments in this thread. – SHAKIR SHABBIR Oct 01 '12 at 11:17