David
David

Reputation: 249

Update 'Enable' of 'asp:DropDownList' in JavaScript

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>

Upvotes: 0

Views: 72

Answers (2)

Tyler Roper
Tyler Roper

Reputation: 21672

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.

Upvotes: 2

Benny
Benny

Reputation: 53

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

Upvotes: 0

Related Questions