添加和QUOT;的onclick"归因于ASP.NET的DropDownList项目 [英] Adding "onclick" attribute to asp.net dropdownlist item
问题描述
我可以在一个单选按钮列表项的属性添加到项目像这样:
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();");
然而,当我试图将属性添加到DropDownList控件它似乎并没有工作。我希望它是相似的。
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.
推荐答案
这不能以同样的方式作为一个RadioButtonList的完成,为一个DropDownList,正确的属性事件名称是onchange事件,而不是onclick事件。
该事件应附DropDownList的本身,而不是项目如下:
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();");
此外,这是有点更复杂,更需要自定义javascript函数根据所选择的选项来执行不同的操作。这里有一个例子:
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);");
自定义JavaScript函数:这个假设的下拉列表项的值是信用卡式和SendPayment
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>
这篇关于添加和QUOT;的onclick&QUOT;归因于ASP.NET的DropDownList项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!