Jump to content

Problem with Dropdownlist SelectedIndexChange event doesn't fire

- - - - -

  • Please log in to reply
4 replies to this topic

#1
ramo2712

ramo2712

    Newbie

  • Members
  • Pip
  • 5 posts
Hi erveryone, I have a Dropdownlist like this:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
onselectedindexchanged="DropDownList1_SelectedInde xChanged">
</asp:DropDownList>

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("DropDownList1_SelectedIndexChanged Fire");
}

Then I have a fucntion on javascript where I bind some data for my DropDownList

<script type="text/javascript" language="javascript">
window.onload = bindDdl;

function bindDdl()
{
// Create an Option object 
var opt = document.createElement("option");

// Add an Option object to Drop Down/List Box
document.getElementById("DropDownList1").options.a dd(opt); 
// Assign text and value to Option object
opt.text = "Hola Mundo";
opt.value = "Hola Mundo";

var opt1 = document.createElement("option");

// Add an Option object to Drop Down/List Box
document.getElementById("DropDownList1").options.a dd(opt1);
// Assign text and value to Option object
opt1.text = "Hola Mundo1";
opt1.value = "Hola Mundo1";
}
</script>
The problem is that when I select an item from the DropDownList, I get this error:

Argument for repayment or invalid callback. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <% @ Page EnableEventValidation = "true"%> in a page.

I do what the error says put it in the web.config <pages enableEventValidation="false"/> and not get the error but the SelectedIndexChanged does not fire the change in the page directive <% @ Page EnableEventValidation = "false" %> and not fired, and then I change in both and shoots. Any suggestions in this regard.

Edited by WingedPanther, 30 August 2010 - 05:00 PM.
add code tags (the # button)


#2
ramo2712

ramo2712

    Newbie

  • Members
  • Pip
  • 5 posts
Help please

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,083 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

The problem is that when I select an item from the DropDownList, I get this error:
So the items you add with Javascript are actually added ("Hola Mundo" and "Hola Mundo1")? :blink:
I don't think it can be correct because your id you use with the Document.getElementById() is wrong.
Asp will always add _ctl01 or something else behind the ID you give. --> only controls that are runat="server"
You should be using
Document.getElementById(<%=DropDownList1.ClientID%>)
If i'm not mistaken. (No idea how it then will be handled as that is not databound, impossible with Javascript).

is there something in yourpage_load?

I noticed this little space but that's probably a little mistake by posting the code:
onselectedindexchanged="DropDownList1_SelectedInd[COLOR="red"]e x[/COLOR]Changed"
What the error suggest is probably to set the validation false, not true... But that's not really a good fix.

#4
ramo2712

ramo2712

    Newbie

  • Members
  • Pip
  • 5 posts
Yes it is posible to say documentGetElementById("DropDownList1"); in fact if there were no posible I wouldn't select an item. You can tested your self if you want, create a new proyect put a DropDownList, set the property AutoPostBack to true, and there double clic on it for automatically create the SelectedIndexChange, and there put the javascript function that i post bindDdl() and thats all, you can lock yourself.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,083 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java

Quote

You can tested your self if you want
Yes, but i'm lazy :p

Anyway. I think the biggest problem is the fact that your data isn't and never will be databound to the dropdownlist. The options you create only exist on the clientSide and not on the serverside. (Because the code works fine with normally added options).
So you can:
  • just hard code them on the .aspx page by adding <ListItem>s.
  • add them in the page_load.

  • Or a huge, totally unnecessary, workaround would be:
add <asp:HiddenField ID="HiddenField1" runat="server"></asp:HiddenField> on your page.
Change the dropdownlist like
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" [B][COLOR="red"]onchange="JavaScript:Do()"[/COLOR][/B] onselectedindexchanged="DropDownList1_SelectedIndexChanged">            

</asp:DropDownList>

In the JavaScript add:
function Do() {

            var ddl = document.getElementById("DropDownList1");

            var hidden = document.getElementById("HiddenField1");


            hidden.value = ddl.options[ddl.selectedIndex].value;

        }

Handle the selectedIndexChange in the page load manually:
protected void Page_Load(object sender, EventArgs e)

        {

            if (Page.IsPostBack)

            {

                Response.Write(HiddenField1.Value + " is clicked.");

            }


        }

And finally the eventvalidation must be set to false for that page.
EnableEventValidation="false"




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users