0

In a aspx page, I am trying to to validate when the user is checking a checkbox, if the select value of the same line has a certain value (the default one) to fire an alert message and uncheck the checkbox. I would like to realise this in javascript to avoid a postback but if possible without jquery.

<asp:ListView ID="lstView_retrievedIPPhones" runat="server" OnDataBound="lstView_retrievedIPPhones_DataBound" OnItemDataBound="lstView_retrievedIPPhones_ItemDataBound">
        <LayoutTemplate>
                    <table id="tbl1" runat="server" class="bordered">
                        <tr id="tr1" runat="server">
                            <th id="th1" runat="server"><asp:CheckBox ID="chkbox_checkAllRows" runat="server" OnClick="javascript:ToogleCheckAll(this.id)"/></th>
                            <th id="th9" runat="server">Sites</th>                                
                            <th id="th8" runat="server">Result</th>
                        </tr>
                        <tr id="ItemPlaceholder" runat="server">  
                        </tr>
                    </table>
                </LayoutTemplate>
                <ItemTemplate>
                    <tr>
                        <td><asp:CheckBox ID="chkbox_toConfigure" runat="server" OnClick="javascript:btnclick()" /></td>
                        <td>
                            <asp:DropDownList ID="dpdown_Site" runat="server"></asp:DropDownList>
                        </td>
                        <td><asp:Image ID="imgResultLastOp" tooltip= "" runat="server" /></td>
                    </tr>
                </ItemTemplate>

        <EmptyDataTemplate></EmptyDataTemplate>
    </asp:ListView>

In the above code, when clicking on chkbox_toConfigure, I want to validate that the DropDownList selectedvalue is not the default one.

Your help would be greatly appreciated.

Salim
  • 495
  • 3
  • 20

2 Answers2

0

If you know the Value of the default item of dropdown (for example its -1 here), then you can check on click of checkbox and alert the user to select another item from dropdown:

Adding this code just before the ending ItemTemplate tag: </ItemTemplate>

<script>
function CheckSelectedItem() {
    var ddl = document.getElementById("<%# Container.FindControl("dpdown_Site").ClientID %>");
    var ddlValue = ddl.options[ddl.selectedIndex].value;
    if(ddlValue == "-1")
        alert("Select another item!");
}

document.getElementById("<%# Container.FindControl("chkbox_toConfigure").ClientID %>").onclick = function() {
    CheckSelectedItem();
    return false;
}
</script>

Some help

Community
  • 1
  • 1
Umair
  • 4,864
  • 3
  • 28
  • 47
  • Hi Umair, Thanks for your answer. Much appreciated. When inserting the code into my , I got the following error: "The name 'dpdown_Site' does not exist in the current context" – Salim Sep 21 '13 at 04:02
  • Ah sorry, that you could do in repeater, here you can use <%# Container.FindControl("chkbox_toConfigure").ClientID %> and <%# Container.FindControl("dpdown_Site").ClientID %> I have edited the answer – Umair Sep 21 '13 at 08:27
  • Hi thanks a lot for your answer! It actually works fine. May I abuse and ask what if I wanted to put it in my global javascript section (on the top of my page)? I execute the below function on checkbox click and tried the blow but did not manage: `function CheckSelectedItem(Chkid) { var ddl = document.getElementById("<%# lstView_retrievedIPPhones.FindControl("dpdown_Site").ClientID %>"); var ddlValue = ddl.options[ddl.selectedIndex].value; if (ddlValue == "<>") alert("Please select a site for the IP Phone"); }` – Salim Sep 21 '13 at 12:28
  • You have to create only 1 function here, and then in the onclick you have to pass ID for both checkbox and dropdown, and then use those ids from parameters and remove this: <%# lstView_retrievedIPPhones.FindControl("dpdown_Site").ClientID %> – Umair Sep 21 '13 at 16:08
  • Hi, Sorry, I can't get it to work: javascript:ToogleCheckAll(this.id, <%# Container.FindControl("dpdown_Site").ClientID %>) gives me an error: The server tag is not well formed. – Salim Sep 22 '13 at 07:40
0

Well I end up doing the following code:

<asp:CheckBox ID="chkbox_toConfigure" runat="server" OnClick='<%# "javascript:CheckSelectedItem(this.id," + ((ListViewDataItem)Container).FindControl("dpdown_Site").ClientID + ");" %>'/>

Into my javascript section :

    function CheckSelectedItem(checkboxID, dropdown) {
    var checkbox = document.getElementById(checkboxID);
    var dropdownValue = dropdown.options[dropdown.selectedIndex].text;
    if (dropdownValue == "<>") {
        alert("Please select a valid value");
        checkbox.checked = false;
    }
}
Salim
  • 495
  • 3
  • 20