I can add an attribute to items in a RadioButtonList item like so:
PaymentMethodDropDownList.Items[0].Attributes.Add("onclick", "javascript:showNoMethods();");
PaymentMethodDropDownList.Items[1].Attributes.Add("onclick", "javascript:showCreditCardMethod();");
PaymentMethodDropDownList.Items[2].Attributes.Add("onclick", "javascript:showSendPaymentMethod();");
However, when I try to add the attributes to a DropDownList control it doesn't seem to work. I would expect it to be similar.
This cannot be done in the same way as a radioButtonList, for a dropdownlist, the correct attribute event name is "onchange" instead of "onclick". The event should be attached to DropDownList Itself and not the items as follows:
PaymentMethodDropDownList.Attributes.Add("onchange",
"showCreditCardMethod();");
Also, this is a little bit more complicated and requires a custom javascript function to perform a different action depending on the option selected. Here's an example:
PaymentMethodDropDownList.Attributes.Add("onchange",
"handleDropDownEvents(this);");
Custom Javascript function: this assumes that values for the dropdown items are "CreditCard" and "SendPayment".
<script type="text/javascript">
function handleDropDownEvents(e){
if(e.value == "CreditCard"){
showCreditCardMethod();
}
else if(e.value == "SendPayment"){
showSendPaymentMethod();
}
}
</script>
Actualy for a DropDownList in ASP .Net, the property you're looking for is OnSelectedIndexChanged or OnTextChanged . Both does quite the same job.
Hope this help ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With