将“onclick”属性添加到asp.net下拉列表项

我可以为RadioButtonList项中的项添加属性,如下所示:
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控件时,它似乎不起作用.我希望它是相似的.

解决方法

这不能以与radioButtonList相同的方式完成,对于下拉列表,正确的属性事件名称是“onchange”而不是“onclick”.
该事件应附加到DropDownList本身,而不是如下项目:
PaymentMethodDropDownList.Attributes.Add("onchange","showCreditCardMethod();");

此外,这有点复杂,并且需要自定义javascript函数来执行不同的操作,具体取决于所选的选项.这是一个例子:

PaymentMethodDropDownList.Attributes.Add("onchange","handleDropDownEvents(this);");

自定义Javascript函数:这假设下拉项的值是“CreditCard”和“SendPayment”.

<script type="text/javascript">
    function handleDropDownEvents(e){
      if(e.value == "CreditCard"){
         showCreditCardMethod();
      }
      else if(e.value == "SendPayment"){
        showSendPaymentMethod();
      }
    }
</script>

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....