简体   繁体   English

在Asp.net中使用Java禁用控件

[英]Enabling Disabling controls with Javascript in Asp.net

I have a fileupload control, Calendar control and a Textbox control in asp.net, which should be enabled or disabled on selection of a check box. 我在asp.net中有一个fileupload控件,一个Calendar控件和一个Textbox控件,应在选中复选框后启用或禁用它们。 I have included a javascript function to perform the same, for asp:Textbox I am able to perform enable disable successfully however for RadDatePicker and AsyncFileUpload enable disable is not applied. 我已经包含了一个执行相同功能的javascript函数,对于asp:Textbox我能够成功执行启用禁用,但是未应用RadDatePicker和AsyncFileUpload启用禁用。

The below are the controls 以下是控件

<telerik:RadDatePicker ID="cmgStartDate">
<ajaxToolkit:AsyncFileUpload ID="AsyncFileUploadComingSoon">
<asp:TextBox ID="txtcmgStartTime">

<asp:CheckBox ID="chkIsFutureMovie" OnChange="javascript:enablecmgSoonControls()"/>

The below is javascript function called when checkbox is checked/unchecked.. 以下是选中/取消选中复选框时调用的javascript函数。

    <script type="text/javascript" language="javascript">
        function enablecmgSoonControls() {
            var imageUpload = "<%= AsyncFileUploadComingSoon.ClientID %>";
            var cmgsoonStartDate = "<%= cmgStartDate.ClientID %>";
            var cmgText = "<%= txtcmgStartTime.ClientID %>";

            if (document.getElementById("<%= chkIsFutureMovie.ClientID %>").checked == true) {
                document.getElementById(imageUpload).disabled = false;
                document.getElementById(cmgsoonStartDate).disabled = false;
                document.getElementById(cmgText).disabled = false;
            }
            else {
                document.getElementById(imageUpload).disabled = true;
                document.getElementById(cmgsoonStartDate).disabled = true;
                document.getElementById(cmgText).disabled = true;
            }
        }
</script>

I tried by identifying the control with $find, it didn't work. 我尝试用$ find标识控件,但没有用。 What am I missing here.. 我在这里想念什么..

Rather than writing 而不是写作

document.getElementById(<%= txtcmgStartTime.ClientID %>")

Try using: 尝试使用:

document.getElementById("cmgstartDate")
document.getElementById("AsyncFileUploadComingSoon")

I think this will work because ultimately the code which you've written will appear as html. 我认为这会起作用,因为最终您编写的代码将显示为html。 Do one thing more when you run your website do inspect element of controls and confirm whether the Id you've created and ID we're getting in html are same or not. 当您运行您的网站时,还要做一件事:检查控件的元素,并确认您创建的ID和我们在html中获得的ID是否相同。

I was able to successfully enable/disable Ajax control by specifying its elementId directly. 通过直接指定其elementId,我能够成功启用/禁用Ajax控件。 However, the major problem was with Telerik control. 但是,主要问题是Telerik控制。 I have modified my script as below to enable/disable <telerik:RadDatePicker ID="cmgStartDate"> . 我已经如下修改了脚本,以启用/禁用<telerik:RadDatePicker ID="cmgStartDate"> Hope it helps someone. 希望它可以帮助某人。

<script type="text/javascript" language="javascript">
        function enablecmgSoonControls() {

            var picker = $find("<%=cmgStartDate.ClientID %>")
            var cmgText = "<%= txtcmgStartTime.ClientID %>";

            if (document.getElementById("<%= chkIsFutureMovie.ClientID %>").checked == true) {
                document.getElementById(cmgText).disabled = false;
                picker.set_enabled(true);
                picker.get_popupButton().disabled = false;
                picker.get_popupButton().className = "rcCalPopup";
            }
            else {
                document.getElementById(cmgText).disabled = true;
                picker.set_enabled(false);
                picker.get_popupButton().disabled = true;
                picker.get_popupButton().className = "rcCalPopup rcDisabled";
            }
        }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM