0

I have 2 DropDownLists, and I am trying to switch the second one to Enable=true, When in the first DropDownList I have choose option number 2.

When I run the code on VS, I receive this error: "JavaScript runtime error: Unable to set property 'setAttribute' of undefined or null reference"

<script language="javascript">
    function Test(ddlId) {

        var ControlName = document.getElementById(ddlId.id);
        if (ControlName.value == 2) {
            alert("good");
            document.getElementById("ddlActivitiesParams2").setAttribute("Enable", true);
            //document.getElementById("ddlActivitiesParams2").disabled = true;
        }
    }
</script>
<asp:TemplateColumn HeaderText="Param1">
    <HeaderStyle Width="25%"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center" Width="25%"></ItemStyle>
    <ItemTemplate>
        <asp:Label ID="lblParam1Desc" runat="server" Text='' Width="90%" />
    </ItemTemplate>
    <FooterTemplate>
        <asp:DropDownList ID="ddlAgentParams" onchange="Test(this);" DataValueField="Value" DataTextField="Text" DataSource=' <%# GetThresholdsAgentTypeParams() %> ' runat="server" Width="90%">
        </asp:DropDownList>
    </FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Param2">
    <HeaderStyle Width="25%"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center" Width="25%"></ItemStyle>
    <ItemTemplate>
        <asp:Label ID="lblParam2Desc" runat="server" Text='aaa' Width="90%" />
    </ItemTemplate>
    <FooterTemplate>
        <asp:DropDownList ID="ddlActivitiesParams2" Enabled="false" Visible="true" DataValueField="Value" DataTextField="Text" DataSource=' <%# GetActivities() %> ' runat="server" Width="90%">
        </asp:DropDownList>
    </FooterTemplate>
</asp:TemplateColumn>

5
  • The attribute 'Enable' does not exists at runtime, only in the design. That is why JavaScript gives an error. Just look at the HTML source and you will see that it is not present. Commented Aug 1, 2016 at 9:42
  • Ok, how can I solve this problem? Commented Aug 1, 2016 at 9:53
  • Something like this: document.getElementById("<%=DropDownList1.ClientID %>").disabled = true;. You have to get the ID generated for the element you want to disable into javascript. Commented Aug 1, 2016 at 10:25
  • You mean: "<%=ddlActivitiesParams2.ClientID %>" ? Because if yes, I have an error. Commented Aug 1, 2016 at 10:48
  • Compiler Error Message: CS0103: The name 'ddlActivitiesParams2' does not exist in the current context Commented Aug 1, 2016 at 11:22

2 Answers 2

2

You're looking for the wrong ID. ASP.Net client-side ID'S will change at runtime. You can't use a server side block in your selector because the control you're trying to access is within another control.

Being that your ddl is in a footer template, I'm assuming there is only one of them. If this is true and youre using a version of ASP.Net that supports it, you can put the attribute ClientIDMode="static" on the ddl, and the ID will not change at runtime.

Otherwise, you may just want to add a unique cssclass to the drop down and select it based on class instead of ID.

Sign up to request clarification or add additional context in comments.

Comments

0

It is often the case that containers change the id of controls in the rendering process. Do a view source on the returned HTML and check what is the id of the created select element

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.